본문 바로가기

BFS36

[백준] 1967번 - 트리의 지름 (파이썬) # 트리의 지름 from collections import deque import sys input = sys.stdin.readline V = int(input()) arr = [[] for _ in range(V+1)] for _ in range(V): temp = list(map(int, input().split())) v = temp[0] for i in range(1, len(temp)-2, 2): arr[v].append((temp[i], temp[i+1])) far_node = 0 def find(a, c): global far_node q = deque() q.append((a, c)) visited = [False] * (V+1) visited[a] = True result = 0 whi.. 2022. 1. 27.
[백준] 2573번 - 빙산 (파이썬) # 빙산 from collections import deque import sys input = sys.stdin.readline N, M = map(int, input().split()) arr = [] ice = [] melt = [] for i in range(N): arr.append(list(map(int, input().split()))) for j in range(M): if arr[i][j] != 0: ice.append((i, j)) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def melt_check(): for x, y in ice: if arr[x][y] != 0: zero = 0 for k in range(4): nx = x + dx[k] ny = y + .. 2022. 1. 25.
[백준] 1707번 - 이분 그래프 (파이썬) # 이분 그래프 import sys from collections import deque input = sys.stdin.readline def bfs(start): global result q = deque() q.append(start) visited[start] = 0 while q: node = q.popleft() for nnode in arr[node]: if visited[nnode] == -1: visited[nnode] = visited[node] % 2 + 1 q.append(nnode) else: if visited[nnode] == visited[node]: result = 'NO' return T = int(input()) for _ in range(T): V, E = map(in.. 2022. 1. 25.
[백준] 16236번 - 아기 상어 (파이썬) # 아기 상어 import sys from collections import deque MAX = sys.maxsize input = sys.stdin.readline N = int(input()) arr = deque() fish = [] now_x = 0 now_y = 0 for i in range(N): arr.append(list(map(int, input().split()))) for j in range(N): if arr[i][j] != 0 and arr[i][j] != 9: fish.append((i, j)) elif arr[i][j] == 9: now_x = i now_y = j arr[i][j] = 0 dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] fish.sort(.. 2022. 1. 23.
[백준] 2206번 - 벽 부수고 이동하기 (파이썬) # 벽 부수고 이동하기 import sys from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) arr = [] for i in range(N): arr.append(list(map(int, input().strip()))) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def bfs(): global result q = deque() q.append((0, 0, 0)) time = [[[0] * 2 for _ in range(M)] for _ in range(N)] time[0][0][0] = 1 while q: x, y, wall = q.popleft() if x == N-.. 2022. 1. 23.
[백준] 1987번 - 알파벳 (파이썬) # 알파벳 import sys input = sys.stdin.readline R, C = map(int, input().split()) arr = [] for _ in range(R): arr.append(list(input().strip())) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] result = 0 def move(): global result q = set([(0, 0, arr[0][0])]) while q: x, y, hist = q.pop() result = max(result, len(hist)) for k in range(4): nx = x + dx[k] ny = y + dy[k] if 0 2022. 1. 23.
[백준] 11559번 - Puyo Puyo (파이썬) # Puyo Puyo from collections import deque import sys input = sys.stdin.readline N = 12 M = 6 dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] arr = [] for _ in range(12): arr.append(list(input().strip())) result = 0 def check(): isExplode = False global result visited = [[False] * M for _ in range(N)] for i in range(N): for j in range(M): if arr[i][j] != '.' and not visited[i][j]: color = arr[i][j] exp = [.. 2022. 1. 22.