목록분류 전체보기 (91)
눈송이의 개발생활
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bsDvXh/btrtTLAyY0D/mrmHedSQZlkWtBzkfT0bn1/img.png)
문제 https://www.acmicpc.net/problem/11725 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(): queue = deque() queue.append(1) while queue: node = queue.popleft() for i in graph[node]: if parent[i] == 0: parent[i] = node queue.append(i) N = int(input()) graph = [[] for _ in range(N+1)] parent = [0 for _ in range(N+1)] for _ in range(N-1): a, b = map..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/dmc5ab/btrtNQCDLbb/0wWo9rIjrP8pKcWSJpirN0/img.png)
문제 https://www.acmicpc.net/problem/7576 코드 from collections import deque import sys input = lambda: sys.stdin.readline().strip() def bfs(): day = 0 while queue: day += 1 for _ in range(len(queue)): x, y = queue.popleft() for dx, dy in [(-1, 0), (1, 0), (0, 1), (0, -1)]: nx, ny = x + dx, y + dy if 0
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bFYgUb/btrtDEu7Nfd/47iUHrMYLto2edHOulB5x0/img.png)
문제 https://www.acmicpc.net/problem/14226 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(s, c): queue = deque() queue.append((s, c)) while queue: s, c = queue.popleft() if dist[s][s] == -1: #방문하지 않은 경우, 화면에서 클립보드로 dist[s][s] = dist[s][c] + 1 queue.append((s, s)) if s+c = 0 and dist[s-1][c] == -1: #삭제 dist[s-1][c] = dist[s][c] + 1 queue.append((s-..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/nr3mx/btrtt5Aw3hh/ajM8aNsBrO0QQqvcc7Ct4k/img.png)
문제 https://www.acmicpc.net/problem/13023 코드 import sys input = lambda: sys.stdin.readline().strip() def dfs(v, depth): global ans visited[v] = 1 if depth == 4: ans = True return for i in graph[v]: if visited[i] == 0: dfs(i, depth + 1) visited[i] = 0 N, M = map(int, input().split()) graph = [[] for _ in range(N)] visited = [0 for _ in range(N)] ans = False for _ in range(M): a, b = map(int, input..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/be8fHc/btrtk2csx60/bndHgSKkPHol3pH4euqtmK/img.png)
문제 https://www.acmicpc.net/problem/13549 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() MAX = 100001 N, K = map(int, input().split()) visit_time = [-1] * MAX visit_time[N] = 0 queue = deque() queue.append(N) while queue: x = queue.popleft() if x == K: # 해당 수에 도착 print(visit_time[x]) break if 0
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/dVxLLR/btrtln8lYmb/sZAjOePYfH35wepPfzc0Ik/img.png)
문제 https://www.acmicpc.net/problem/1697 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(): queue = deque() queue.append(N) while queue: x = queue.popleft() if x == K: # 해당 수에 도착 print(visited[x]) break for nx in (x - 1, x + 1, x * 2): if 0
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/c65H2T/btrs7qXuG7B/Ad6bBWGmAibAvEjr8RLKL1/img.png)
문제 https://www.acmicpc.net/problem/2178 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(x, y): queue = deque([(x, y)]) visited[x][y] = 1 while queue: cx, cy = queue.popleft() for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: nx, ny = cx + dx, cy + dy if 0 > nx or nx > N-1 or ny M-1: continue if visited[nx][ny] == 0 and graph[nx][ny] =..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bhg891/btrs3aBG7nX/wBe2RXMK3tHel4klqB21H0/img.png)
문제 https://www.acmicpc.net/problem/4963 코드 from collections import deque import sys input = lambda : sys.stdin.readline().strip() def bfs(x, y): queue = deque([(x, y)]) visited[x][y] = 1 while queue: cx, cy = queue.popleft() for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, 1), (1, -1), (1, 1), (-1, -1)]: nx, ny = cx + dx, cy + dy if nx h-1 or ny w-1 : continue if vis..