https://school.programmers.co.kr/learn/courses/30/lessons/154540 ํ๋ก๊ทธ๋๋จธ์ค์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์
๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์
๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.programmers.co.kr โ
DFS ํ์ดimport syssys.setrecursionlimit(10**6)dx = [1, -1, 0, 0]dy = [0, 0, -1, 1]def dfs(x, y, graph): count = graph[x][y] # ํ์ฌ ์์น ์๋๊ฐ graph[x][y] = 0 # ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ for d in range(4): nx = x + dx[d] ny =..
๐ฅ Silver2 https://www.acmicpc.net/problem/17086 โ
์ ๋ตํ์ดimport sysfrom collections import dequeinput = sys.stdin.readlinem, n = map(int, input().rstrip().split())dx = [0, 1, 1, 1, 0, -1, -1, -1]dy = [-1, -1, 0, 1, 1, 1, 0, -1]def bfs(): result = 0 while queue: cy, cx = queue.popleft() for i in range(8): ny, nx = dy[i] + cy, dx[i] + cx # ์์ด๊ฐ ์๋ 0์ ๋ํด ์ด๋ ..
๐ฅ Silver1https://www.acmicpc.net/problem/1697 ๋ฉ๋ชจ๋ฆฌ์คํ์๊ฐ40316KB252ms โ
์ ๋ตํ์ดimport sysfrom collections import dequeinput = sys.stdin.readline# ์ต๋จ๊ฒฝ๋ก ์ฐพ๊ธฐ=์ต๋จ์๊ฐ ์ฐพ๊ธฐ (bfs)def bfs(start,target): global cur queue=deque([start]) visited[start]=0 #์์์ ์ ์ด๋๊ฑฐ๋ฆฌ=0 while queue: cur=queue.popleft() if cur==target: return visited[cur] for i in range(3): if i ์๋น..
๐ฅ Silver1https://www.acmicpc.net/problem/21937 โ
์ ๋ต์ฝ๋import sysinput = sys.stdin.readlinedef dfs(idx): stack.append(idx) visited[idx] = True while stack: cur = stack.pop() for i in graph[cur]: if not visited[i]: stack.append(i) visited[i] = True answer.add(i)n, m = map(int, input().strip().split())graph = [[] fo..
๐ฅ Silver2 https://www.acmicpc.net/problem/7562 โ
์ ๋ตํ์ดimport sysfrom collections import dequeinput = sys.stdin.readlinedx = [1, 2, 2, 1, -1, -2, -2, -1]dy = [2, 1, -1, -2, -2, -1, 1, 2]def bfs(start_x, start_y, end_x, end_y, length): queue = deque([(start_x, start_y)]) visited = [[-1] * length for _ in range(length)] # ๋ชจ๋ ๊ฐ์ -1๋ก ์ด๊ธฐํ visited[start_x][start_y] = 0 # ์์ ์ง์ ์ ์ด๋ ๊ฑฐ๋ฆฌ 0์ผ๋ก ์ค..
๐ฅ Silver3 https://www.acmicpc.net/problem/2606 โ
์ ๋ตํ์ด : DFSdef dfs(idx): stack=[idx] #stack.append(idx) visited[idx]=True while stack: cur=stack.pop() for adj in graph[cur]: if not visited[adj]: visited[adj]=True stack.append(adj)# ๊ทธ๋ํ ์ด๊ธฐ ์ค์ n=int(input())graph=[[] for _ in range(n+1)] #0๋ฒ์งธ ์ธ๋ฑ์ค๋ ๋น๋ฐฐ์ด๋กfor _ in range(int(input())):..
๐ฅSilver2https://www.acmicpc.net/problem/2644 โ
์ ๋ตํ์ดimport sysfrom collections import dequeinput = sys.stdin.readlinedef bfs(start,end): queue=deque([start]) visited[start]=0 # ์์์ง์ : ๊ฑฐ๋ฆฌ 0 while queue: cur=queue.popleft() # ๋ชฉํ ์ง์ ๋๋ฌํ๋ฉด ๊ฑฐ๋ฆฌ ๋ฐํ if cur==end: return visited[cur] # ์ธ์ ๋
ธ๋ ๋ฐฉ๋ฌธ for neighbor in graph[cur]: if visit..