본문 바로가기

Algorithm/BOJ133

[백준] 2665번 - 미로만들기 (파이썬) # 미로만들기 from collections import deque import sys input = sys.stdin.readline N = int(input()) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] arr = [] for i in range(N): arr.append(list(map(int, input().strip()))) def bfs(): q = deque() q.append((0, 0)) visited = [[-1] * N for _ in range(N)] visited[0][0] = 0 while q: x, y = q.popleft() if x == N-1 and y == N-1: return visited[x][y] for k in range(4): nx =.. 2022. 3. 4.
[백준] 4179번 - 불! (파이썬) # 불! from collections import deque import sys input = sys.stdin.readline dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] N, M = map(int, input().split()) arr = [] now = deque() fire = deque() for i in range(N): arr.append(list(input().strip())) for j in range(M): if arr[i][j] == 'J': now.append((i, j)) arr[i][j] = 1 if arr[i][j] == 'F': fire.append((i, j)) def move(): leng = len(now) for _ in range(leng):.. 2022. 3. 3.
[백준] 2075번 - N번째 큰 수 ( 파이썬) # N번째 큰 수 import sys import heapq input = sys.stdin.readline N = int(input()) heap = [] for i in range(N): temp = list(map(int, input().split())) if i == 0: for x in temp: heapq.heappush(heap, x) continue for x in temp: if heap[0] < x: heapq.heappop(heap) heapq.heappush(heap, x) print(heap[0]) 힙 1. 한 줄씩 입력을 받고 첫 줄은 heapq에 push 2. 둘째 줄 부터는 - heap[0]보다 큰 수만 push - 이때, heap의 크기를 N만큼 유지해주기 위해서 pop 3.. 2022. 3. 2.
[백준] 10775번 - 공항 (파이썬) # 공항 def find_parent(x): if x != parent[x]: parent[x] = find_parent(parent[x]) return parent[x] def union_parent(x, y): x = find_parent(x) y = find_parent(y) if x < y: parent[y] = x else: parent[x] = y G = int(input()) P = int(input()) parent = [i for i in range(G+1)] plane = [] for _ in range(P): plane.append(int(input())) count = 0 for p in plane: x = find_parent(p) if x == 0: break union_pare.. 2022. 3. 1.
[백준] 17472번 - 다리 만들기 2 (파이썬) # 다리 만들기 2 from collections import deque import sys input = sys.stdin.readline MAX = sys.maxsize dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] N, M = map(int, input().split()) land = [] arr = [] for i in range(N): arr.append(list(map(int, input().split()))) for j in range(M): if arr[i][j] == 1: land.append((i, j)) def find_land(visited, a, b, num): q = deque() q.append((a, b)) while q: x, y = q.popleft(.. 2022. 2. 19.
[백준] 1613번 - 역사 (파이썬) # 역사 import sys input = sys.stdin.readline N, M = map(int, input().split()) arr = [[False]*(N+1) for _ in range(N+1)] for a in range(1, N+1): for b in range(1, N + 1): if a == b: arr[a][b] = False for _ in range(M): a, b = map(int, input().split()) arr[a][b] = True for k in range(1, N+1): for i in range(1, N+1): for j in range(1, N+1): if arr[i][k] and arr[k][j]: arr[i][j] = True K = int(input().. 2022. 2. 19.
[백준] 10159번 - 저울 (파이썬) # 저울 import sys input = sys.stdin.readline N = int(input()) M = int(input()) arr = [[False]*(N+1) for _ in range(N+1)] for a in range(1, N+1): for b in range(1, N+1): if a == b: arr[a][b] = False for _ in range(M): a, b = map(int, input().split()) arr[a][b] = True for k in range(1, N+1): for i in range(1, N+1): for j in range(1, N+1): if arr[i][k] and arr[k][j]: arr[i][j] = True for i in range(1.. 2022. 2. 19.