BFS

๋ฌธ์ œ ์š”์•ฝ๋„์—ฐ์ด๋Š” ์บ ํผ์Šค์—์„œ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์นœ๊ตฌ์˜ ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์–ดํ•œ๋‹ค. ์บ ํผ์Šค๋Š” ( N \times M ) ํฌ๊ธฐ์ด๋ฉฐ, ๋„์—ฐ์ด๋Š” ๋นˆ ๊ณต๊ฐ„('O')๊ณผ ์‚ฌ๋žŒ('P')์ด ์žˆ๋Š” ๊ณณ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฒฝ('X')์€ ์ด๋™ํ•  ์ˆ˜ ์—†๋‹ค. ๋„์—ฐ์ด๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜๋Š” 'I'๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋„์—ฐ์ด๊ฐ€ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.์ž…๋ ฅ์ฒซ ๋ฒˆ์งธ ์ค„์— ์บ ํผ์Šค์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ ( N )๊ณผ ( M )์ด ์ฃผ์–ด์ง„๋‹ค.๋‹ค์Œ ( N )๊ฐœ์˜ ์ค„์— ์บ ํผ์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 'O'๋Š” ๋นˆ ๊ณต๊ฐ„, 'X'๋Š” ๋ฒฝ, 'I'๋Š” ๋„์—ฐ์ด์˜ ์‹œ์ž‘ ์œ„์น˜, 'P'๋Š” ์‚ฌ๋žŒ์ด๋‹ค.์ถœ๋ ฅ๋„์—ฐ์ด๊ฐ€ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ์•„๋ฌด๋„ ๋งŒ๋‚˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ "TT"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.  ํ’€์ด ์„ค๋ช…์ด ๋ฌธ์ œ๋Š” BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)๋ฅผ..
๋ฌธ์ œ ์š”์•ฝ์ง€๋„์—์„œ ๊ฐ ์ง€์ ์— ๋Œ€ํ•ด ๋ชฉํ‘œ ์ง€์ (๊ฐ’์ด 2์ธ ์ง€์ )๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ง€๋„๋Š” ๊ฐ€๋กœ์™€ ์„ธ๋กœ๋กœ๋งŒ ์›€์ง์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, 0์€ ๊ฐˆ ์ˆ˜ ์—†๋Š” ๋•…, 1์€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋•…, 2๋Š” ๋ชฉํ‘œ ์ง€์ ์„ ์˜๋ฏธํ•œ๋‹ค. ํ’€์ด ์„ค๋ช…์ด ๋ฌธ์ œ๋Š” ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ง€์ ์—์„œ ๋ชฉํ‘œ ์ง€์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.BFS๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐ ์ ํ•ฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. from collections import dequeimport sysinput = sys.stdin.readlinen, m = map(int, input().split())graph = [list(map(int, input().split())) for _ in range(n)]def bfs(graph, n, m): distan..
๋ฌธ์ œ ์š”์•ฝ๋ณด๋“œ ๊ฒŒ์ž„์—์„œ ํ”Œ๋ ˆ์ด์–ด๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค 1๋ฒˆ ์นธ์—์„œ 100๋ฒˆ ์นธ๊นŒ์ง€ ์ด๋™ํ•ด์•ผ ํ•œ๋‹ค.์‚ฌ๋‹ค๋ฆฌ๋Š” ๋„์ฐฉํ•˜๋ฉด ๋” ๋†’์€ ๋ฒˆํ˜ธ๋กœ ์ด๋™ํ•˜๊ณ , ๋ฑ€์€ ๋” ๋‚ฎ์€ ๋ฒˆํ˜ธ๋กœ ์ด๋™ํ•˜๊ฒŒ ํ•œ๋‹ค.100๋ฒˆ ์นธ์— ๋„์ฐฉํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ์‚ฌ์œ„๋ฅผ ์ตœ์†Œ ๋ช‡ ๋ฒˆ ๊ตด๋ ค์•ผ ํ•˜๋Š”์ง€ ๊ตฌํ•œ๋‹ค. ํ’€์ดfrom collections import dequeimport sysinput = sys.stdin.readlinedef snakes_and_ladders(N, M, ladders, snakes): board = list(range(101)) # ๋ณด๋“œ์˜ ๊ฐ ์นธ ์ดˆ๊ธฐํ™” for start, end in ladders.items(): board[start] = end # ์‚ฌ๋‹ค๋ฆฌ ์„ค์ • for start, end in snakes.it..
๋ฌธ์ œ ์š”์•ฝ๋„ค ๊ฐœ์˜ ๋ช…๋ น์–ด D, S, L, R ๊ฐ€ ์žˆ๋Š” ๊ณ„์‚ฐ๊ธฐ๋‘ ์ •์ˆ˜ A, B๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, A๋ฅผ B๋กœ ๋ฐ”๊พธ๋Š” ์ตœ์†Œํ•œ์˜ ๋ช…๋ น์–ด ์ƒ์„ฑ-> BFSBFS๋ผ๋Š” ๊ฑด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ƒ๊ฐํ•ด๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ.. ์ด ๋ฌธ์ œ์—์„œ ๊ฐ€์žฅ ์–ด๋ ค์› ๋˜ ์ ์€ ์‹œ๊ฐ„ ์ดˆ๊ณผ์˜€๋‹ค. ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ’€์ดfrom collections import dequedef D(n): return (2 * n) % 10000def S(n): return n - 1 if n != 0 else 9999def L(n): n = f"{n:04}" # ๋„ค ์ž๋ฆฟ์ˆ˜๋กœ ๋งž์ถ”๊ธฐ return int(n[1:] + n[0])def R(n): n = f"{n:04}" # ๋„ค ์ž๋ฆฟ์ˆ˜๋กœ ๋งž์ถ”๊ธฐ return int(n[-1] + n[:-1])def bfs(..
ํ’€์ด 1: ํ”Œ๋กœ์ด๋“œ-์›Œ์…œํ”Œ๋กœ์ด๋“œ-์›Œ์…œ: ๋ชจ๋“  ์ •์  ์Œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^3) - ์ด ๋ฌธ์ œ๋Š” N ์ตœ๋Œ“๊ฐ’ 100์ด๋ฏ€๋กœ ์ด ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.import sysinput = sys.stdin.readlineN, M = map(int, input().split())# ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”INF = sys.maxsizedist = [[INF] * (N+1) for _ in range(N+1)]# ์ž์‹ ๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” 0์œผ๋กœfor i in range(1, N+1): dist[i][i] = 0# ์นœ๊ตฌ ๊ด€๊ณ„ ์ž…๋ ฅfor _ in range(M): A, B = map(int, input().split()) dist[A][B] = 1 dist[B][A] = 1# ํ”Œ๋กœ์ด๋“œ-์›Œ์…œfor k ..
๋ฐฉํ–ฅ ์—†๋Š” ๊ทธ๋ž˜ํ”„ ์ฃผ์–ด์ง -> ์—ฐ๊ฒฐ ์š”์†Œ(connected component) ๊ฐœ์ˆ˜ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์•„์ด๋””์–ด๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ (DFS/BFS)๋ฐฉ๋ฌธ ๋ชปํ•œ ๋…ธ๋“œ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด ์นด์šดํŠธ ์ฆ๊ฐ€ํ•˜๊ณ ๊ทธ ๋ฐฉ๋ฌธ ๋ชปํ•œ ๋…ธ๋“œ (false ๋…ธ๋“œ)์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์‹œ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰false ๋…ธ๋“œ ์—†์„ ๋•Œ๊นŒ์ง€ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋ฐ˜๋ณต ํ’€์ด - DFS ์‚ฌ์šฉimport sysinput = sys.stdin.readlineN, M = map(int, input().split())graph = [[] for _ in range(N+1)]for _ in range(M): u, v = map(int, input().split()) graph[u].append(v) graph[v].append(u)visited = [False] * (N+..
๋ฌธ์ œ ์š”์•ฝ์ˆ˜๋นˆ ํ˜„์žฌ ์œ„์น˜ N, ๋™์ƒ ํ˜„์žฌ ์œ„์น˜ K, 0๋ถ€ํ„ฐ 100,000 ๊นŒ์ง€์˜ ์ •์ˆ˜์ˆ˜๋นˆ์ด๊ฐ€ ๋™์ƒ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ.์ˆ˜๋นˆ์ด๊ฐ€ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑด 3๊ฐ€์ง€ : X-1, X+1, 2*X ํ’€์ด์‹œ์ž‘ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋Š” 3๊ฐœ, ๊ฐ ๋…ธ๋“œ์—์„œ 1์ดˆ๊ฐ€ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์ด๋™ํ•˜๋‹ค๊ฐ€,๋ชฉํ‘œ ๋…ธ๋“œ์— ๋„์ฐฉํ•˜๋ฉด ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ ๋ฆฌํ„ด-> BFS๋กœ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋‹ค def bfs(n, k): if n == k: # ๊ฐ™๋‹ค๋ฉด ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ 0 return 0 visited = [False] * 100001 # ์ž…๋ ฅ ๊ฐ’ ๋ฒ”์œ„๋ณด๋‹ค ํฌ๊ฒŒ queue = deque([(n, 0)]) # n์—๋Š” ํ˜„์žฌ ๋…ธ๋“œ ์œ„์น˜, 0์—๋Š” ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ ์ €์žฅ while queue: cur, time = queu..
๋ฌธ์ œ ์š”์•ฝn * m ์‚ฌ์ด์ฆˆ ํ† ๋งˆํ†  ๋ณด๊ด€ ์ƒ์ž์ต์€ ํ† ๋งˆํ†  ์ธ์ ‘ํ•œ ๊ณณ์— ์žˆ๋Š” ๊ฑด ์˜ํ–ฅ ๋ฐ›์•„ ์ต๊ฒŒ ๋จ.์ฐฝ๊ณ ์— ๋ณด๊ด€ํ•œ ํ† ๋งˆํ† ๋“ค์ด ์ตœ์†Œ ๋ฉฐ์น  ์ง€๋‚˜๋ฉด ๋‹ค ์ต๊ฒŒ ๋˜๋Š”์ง€!๋‹จ, ์ƒ์ž์˜ ์ผ๋ถ€ ์นธ์—๋Š” ํ† ๋งˆํ† ๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. M,N์€ 2๋ถ€ํ„ฐ 10001์€ ์ต์€ ํ† ๋งˆํ† , 0์€ ์•ˆ ์ต์€ ํ† ๋งˆํ† , -1์€ ํ† ๋งˆํ†  ์—†์Œํ† ๋งˆํ† ๋Š” ์ตœ์†Œ ํ•˜๋‚˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ชจ๋‘ ์ต์„ ๋•Œ๊นŒ์ง€์˜ ์ตœ์†Œ ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅํ•˜๋˜,๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์–ด์žˆ๋‹ค๋ฉด 0 ์ถœ๋ ฅ, ๋ชจ๋‘ ์ต์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.  ํ’€์ด์ด ๋ฌธ์ œ๋Š” BFS๋กœ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋‹ค. ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ์˜ํ–ฅ์„ ๋ฐ›์•„ ์ต๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ. from collections import dequem, n = map(int, input().split()) # ๊ฐ€๋กœ, ์„ธ๋กœgraph = []for i in..
ํ’€์ด 1: DFS, ์žฌ๊ท€์ด์šฉ - RecursionErrordef dfs(x, y): visited[x][y] = True for dx, dy in [(1,0), (-1,0), (0,1), (0,-1)]: nx, ny = x + dx, y + dy if 0 ์ฒซ๋ฒˆ์งธ ํ’€์ด์ด๋‹ค. DFS์™€ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์—ˆ๋”๋‹ˆ Recursion Error๊ฐ€ ๋‚ฌ๋‹ค.Recursion Error: ์žฌ๊ท€์™€ ๊ด€๋ จ๋œ ์—๋Ÿฌ. ์ฃผ๋กœ Python์ด ์ •ํ•œ ์ตœ๋Œ€ ์žฌ๊ท€ ๊นŠ์ด๋ณด๋‹ค ๋” ๊นŠ์–ด์งˆ ๋•Œ.์ด ๊ฒฝ์šฐ, ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ๋ฐ”๊พธ์–ด์•ผ ํ•œ๋‹ค.BFS๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€๊ฑฐ๋‚˜, ์žฌ๊ท€ ์—†์ด DFS๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ์ฐธ๊ณ  ๋งํฌ : https://yesolz.tistory.com/entry/Python-%EB%B0%B1%EC%A..
1. n, m, v ์ž…๋ ฅ ๋ฐ›๊ธฐ (๋„์–ด์“ฐ๊ธฐ ์ž…๋ ฅ)n, m, v = map(int, input().split())N(์ •์ ์ˆ˜), M(๊ฐ„์„ ์ˆ˜), V(ํƒ์ƒ‰ ์‹œ์ž‘ํ•  ์ •์  ๋ฒˆํ˜ธ) 2. ๊ทธ๋ž˜ํ”„ ์ž…๋ ฅ ๋ฐ›๊ธฐ (๋…ธ๋“œ ์—ฐ๊ฒฐ ์ •๋ณด) - ์ธ์ ‘ ํ–‰๋ ฌgraph = [[0] * (n+1) for _ in range(n+1)]for i in range(m): a, b = map(int, input().split()) graph[a][b] = graph[b][a] = 1 ์ •์  ๋ฒˆํ˜ธ์™€ ์ธ๋ฑ์Šค ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด n+1๋กœ ์ดˆ๊ธฐํ™”๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ graph[a][b] = graph[b][a] = 1 ** ๊ทธ๋ž˜ํ”„ ์ถœ๋ ฅํ•˜๊ธฐdef print_graph(graph): for row in graph[1:]: # 0๋ฒˆ์งธ ํ–‰๊ณผ ์—ด์€ ์‚ฌ์šฉํ•˜..
yesolz
'BFS' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก