목록백준 문제 풀이 (154)
황소개발자
고진감래다 import sys from collections import deque input = sys.stdin.readline w, h = map(int, input().split()) miro = [[] for i in range(h)] for i in range(h): ss = input().strip() for s in ss: miro[i].append(int(s)) dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] ans = 999999999 def bfs(y, x): q = [[y, x]] nxtq = [] miro[y][x] = 2 while q: now = q.pop(0) for i in range(4): ny = now[0] + dy[i] nx = now[1] + d..
불가능이란 없다 n, k = map(int, input().split()) MAX = -1 dist = [-1 for i in range(100001)] def bfs(v): q = [v] dist[v] = 0 nxt_q = [] while q: now = q.pop(0) if now * 2
더 화이팅하자 s = int(input()) MAX = 999999999 time = [[MAX for i in range(1001)] for j in range(1001)] # 1, 0 def bfs(s, c): q = [[s, c]] time[s][c] = 0 while q: now = q.pop(0) s_now = now[0] c_now = now[1] if time[s_now][s_now] == MAX: time[s_now][s_now] = time[s_now][c_now] + 1 q.append([s_now, s_now]) if s_now + c_now = 0 and time[s_now - 1][c_now] == MAX: time[s_now - 1][c_now] = time[s_now][c_no..
화이팅! n, k = map(int, input().split()) dist = [-1 for i in range(100001)] from collections import deque def bfs(v): q = deque() q.append(v) dist[v] = 0 while q: now = q.popleft() if now + 1 = 0 and dist[now - 1] == -1: q.append(now - 1) dist[now - 1] = dist[now] + 1 if now * 2
리스트에서 pop할 때, 시간복잡도는 O(n) 이다. 그러나 콜렉션에 디큐에서 pop의 시간복잡도는 O(1) 로 구현되어있다. import sys from collections import deque input = sys.stdin.readline dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] w, h = map(int, input().split()) mat = [] for i in range(h): mat.append(list(map(int, input().split()))) q = deque() for i in range(h): for j in range(w): if mat[i][j] == 1: q.append([i, j]) def bfs(): global q while q: n..
import sys input = sys.stdin.readline h, w = map(int, input().split()) mat = [[] for i in range(h)] dx = [1, -1 , 0, 0] dy = [0, 0, 1, -1] for i in range(h): ss = input().strip() for s in ss: mat[i].append(int(s)) def bfs(y, x): q = [[y, x]] while q: now = q.pop(0) for i in range(4): ny = now[0] + dy[i] nx = now[1] + dx[i] if 0
import sys input = sys.stdin.readline dx = [1, -1, 0, 0, 1, 1, -1, -1] dy = [0, 0, 1, -1, 1, -1, 1, -1] def bfs(y, x, count): q = [[y, x]] while q: now = q.pop(0) for i in range(8): ny = now[0] + dy[i] nx = now[1] + dx[i] if 0
import sys input = sys.stdin.readline dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] def bfs(y, x, count, total): q = [[y, x]] mat[y][x] = count total += 1 while q: now = q.pop(0) for i in range(4): ny = now[0] + dy[i] nx = now[1] + dx[i] if 0