[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2644 - ์ดŒ์ˆ˜๊ณ„์‚ฐ

keyneneยท2022๋…„ 11์›” 4์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
23/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2644 - ์ดŒ์ˆ˜๊ณ„์‚ฐ

๐Ÿ“œ๋ฌธ์ œ




DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)

๊ทธ๋ž˜ํ”„์—์„œ ๊นŠ์€ ๋ถ€๋ถ„์„ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
#Stack #์Šคํƒ #์žฌ๊ท€ #์ˆœํ™˜ #๋ฌดํ•œ๋ฃจํ”„ํƒˆ์ถœ #Back-Tracking

BFS(๋„“์ด์šฐ์„ ํƒ์ƒ‰)

๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
#Queue #ํ


๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

DFS์•Œ๊ณ ๋ฆฌ์ฆ˜

n,s(start),e(end),m,๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•˜๊ณ , dfs()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด start==end์ผ ๋•Œ, dep์„ ์ถœ๋ ฅํ•˜์ž

BFS์•Œ๊ณ ๋ฆฌ์ฆ˜

n,s(start),e(end),m,๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•˜๊ณ , bfs()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด res์— ์ดŒ์ˆ˜๋ฅผ ์ €์žฅํ•ด๊ฐ€๋ฉด์„œ
ํ๊ฐ€ ๋น„์—ˆ์„ ๋•Œ res๋ฅผ ์ถœ๋ ฅํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

DFS์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. n,s(start),e(end),m,๋…ธ๋“œ๋“ค์„ MAP์œผ๋กœ ์ธ๋ฑ์‹ฑํ•˜์—ฌ ์ €์žฅ, visited(list)๋ฅผ 1e9๋กœ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ
    dfs()์—์„œ start == end์ผ ๋•Œ, visited[end]๊ฐ’์„ ์ถœ๋ ฅํ•˜์ž
โ“ visited๋ฅผ 1e9๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ด์œ 

  dfs()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค dep๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธธ๊ป€๋ฐ,
  visited[end]์— ์ €์žฅ๋œ ๊ฐ’๊ณผ dep๋ฅผ ๋น„๊ตํ•˜์—ฌ 
  ๋” ์ž‘์€ ๊ฐ’์„ ์ตœ์ข… visited[end]์— ๋„ฃ์–ด ๋ฐ˜ํ™˜ํ• ๊บผ๋‹ˆ๊นŒ!

BFS์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. n,s(start),e(end),m,๋…ธ๋“œ๋“ค์„ MAP์œผ๋กœ ์ธ๋ฑ์‹ฑํ•˜์—ฌ ์ €์žฅ, visited(list)๋ฅผ False๋กœ, res(list)๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์ž
  2. bfs()์— start๊ฐ’์„ ํ์— ๋„ฃ์–ด, ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉด์„œ ๋ฐฉ๋ฌธํ•œ์ ์ด ์—†์œผ๋ฉด res๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค์ž
  3. bfs()ํ•จ์ˆ˜ ์ข…๋ฃŒ ํ›„ res[end] > 0์ด๋ฉด ๋ฐฉ๋ฌธํ•œ์ ์ด ์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ํ˜ธ์ถœ,
    ์•„๋‹ˆ๋ฉด "-1"์„ ์ถœ๋ ฅํ•˜์ž

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

DFS์•Œ๊ณ ๋ฆฌ์ฆ˜

import sys
input = sys.stdin.readline

n = int(input().rstrip())
start,end = map(int, input().split())
m = int(input().rstrip())
node = [map(int, input().split())for _ in range(m)]
visited = [1e9]*(n+1)

#์ธ๋ฑ์‹ฑํ•˜๊ธฐ
amap = [[] for _ in range(n+1)]
for x,y in node:
    amap[x].append(y)
    amap[y].append(x)

#DFS
def dfs(start, end, amap, dep):
    if start == end:
        visited[end] = min(visited[end],dep)
        return

    for i in amap[start]:
    	#๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†์œผ๋ฉด ๋ฐฉ๋ฌธํ•˜์—ฌ dep ์ €์žฅ
        if visited[i] > dep:
            visited[i] = dep
            dfs(i, end, amap, dep+1)

dfs(start, end, amap, 1)

#visited[end]๊ฐ’์ด 1e9์ด๋ฉด ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋Š” ๋œป์ด๋‹ˆ๊นŒ
if visited[end] == 1e9:
    print(-1)
else:
    print(visited[end])

BFS์•Œ๊ณ ๋ฆฌ์ฆ˜

import sys
from collections import deque
input = sys.stdin.readline

n = int(input())
start, end = map(int, input().split())
m = int(input())
node = [list(map(int, input().split())) for _ in range(m)]
visited = [False]*(n+1)
res = [0]*(n+1)

#์ธ๋ฑ์‹ฑํ•˜๊ธฐ
rel = [[] for _ in range(n+1)]
for u,v in node:
    rel[u].append(v)
    rel[v].append(u)

#BFS
def bfs(start):
    queue = deque()
    queue.append(start)
    visited[start] = True
	
    #ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์‹คํ–‰
    while queue:
    	#์ œ์ผ ๋จผ์ € ๋“ค์–ด์˜จ ๊ฐ’๋ถ€ํ„ฐ ๊ฒ€์‚ฌ
        v = queue.popleft()
        
        for i in rel[v]:
            if not visited[i]:
                queue.append(i)
                #ํ•จ์ˆ˜ ๋ฐ–์— ์žˆ๋Š” res์— ํ˜„์žฌ๋…ธ๋“œ(v)์™€์˜ ์ดŒ์ˆ˜๋ฅผ ์ €์žฅ
                res[i] = res[v]+1
                visited[i] = True

bfs(start)
#res[end]๊ฐ’์ด 0์ด๋ฉด ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋Š” ๋œป์ด๋‹ˆ๊นŒ
if res[end] > 0:
    print(res[end])
else:
    print(-1)

๐Ÿ“š์ •๋ฆฌ

๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1260 - DFS์™€ BFS
1. DFS์™€ BFS์ค‘ ์„ ํƒ, ์ธ๋ฑ์‹ฑ/์ธ์ ‘ํ–‰๋ ฌ์ค‘ ์„ ํƒ์ด ๊ด€๊ฑด์ธ ๊ฒƒ ๊ฐ™๋‹ค.
(์œ„ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค˜๋“ฏ์ด ๋ฐฉ๋ฒ•์ด ๋„ˆ๋ฌด ๋‹ค์–‘ํ•ด์„œ ์„ค๊ณ„ ์‹œ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ๊ด€๊ฑด)

profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€