[Baekjoon/π₯β ’] 2206: λ²½ λΆμκ³ μ΄λνκΈ°βοΈ
πββοΈ λ²½ λΆμκ³ μ΄λνκΈ°
NΓMμ νλ ¬λ‘ ννλλ λ§΅μ΄ μλ€. 맡μμ 0μ μ΄λν μ μλ κ³³μ λνλ΄κ³ , 1μ μ΄λν μ μλ λ²½μ΄ μλ κ³³μ λνλΈλ€. λΉμ μ (1, 1)μμ (N, M)μ μμΉκΉμ§ μ΄λνλ € νλλ°, μ΄λ μ΅λ¨ κ²½λ‘λ‘ μ΄λνλ € νλ€. μ΅λ¨κ²½λ‘λ 맡μμ κ°μ₯ μ μ κ°μμ μΉΈμ μ§λλ κ²½λ‘λ₯Ό λ§νλλ°, μ΄λ μμνλ μΉΈκ³Ό λλλ μΉΈλ ν¬ν¨ν΄μ μΌλ€.
λ§μ½μ μ΄λνλ λμ€μ ν κ°μ λ²½μ λΆμκ³ μ΄λνλ κ²μ΄ μ’ λ κ²½λ‘κ° μ§§μμ§λ€λ©΄, λ²½μ ν κ° κΉμ§ λΆμκ³ μ΄λνμ¬λ λλ€.
ν μΉΈμμ μ΄λν μ μλ μΉΈμ μνμ’μ°λ‘ μΈμ ν μΉΈμ΄λ€.
λ§΅μ΄ μ£Όμ΄μ‘μ λ, μ΅λ¨ κ²½λ‘λ₯Ό κ΅¬ν΄ λ΄λ νλ‘κ·Έλ¨μ μμ±νμμ€.
πββοΈ μ λ ₯
첫째 μ€μ N(1 β€ N β€ 1,000), M(1 β€ M β€ 1,000)μ΄ μ£Όμ΄μ§λ€. λ€μ Nκ°μ μ€μ Mκ°μ μ«μλ‘ λ§΅μ΄ μ£Όμ΄μ§λ€. (1, 1)κ³Ό (N, M)μ νμ 0μ΄λΌκ³ κ°μ νμ.
πββοΈ μΆλ ₯
첫째 μ€μ μ΅λ¨ 거리λ₯Ό μΆλ ₯νλ€. λΆκ°λ₯ν λλ -1μ μΆλ ₯νλ€.
π©βπ» μ΄λ»κ² νμ΄λκ°κΉ?
μ°μ κ·Έλν νμ, μ΅λ¨κ±°λ¦¬ λ¬Έμ μ΄κΈ° λλ¬Έμ bfsλ₯Ό μ¬μ©ν μ μλ€.
ν΄λΉ λ¬Έμ λ μΌλ° bfs λ¬Έμ μμ μ‘°κ±΄μ΄ μΆκ°λ λ¬Έμ μ΄λ€.
λ§μ½μ μ΄λνλ λμ€μ ν κ°μ λ²½μ λΆμκ³ μ΄λνλ κ²μ΄ μ’ λ κ²½λ‘κ° μ§§μμ§λ€λ©΄, λ²½μ ν κ° κΉμ§ λΆμκ³ μ΄λνμ¬λ λλ€.
μ²μμλ λ²½μ μ’νλ₯Ό λͺ¨λ κΈ°λ‘ν΄ κ°κ°μ κ²½μ°μμ λ«μμ λ bfs λ°λ³΅ν΄μ νΈμΆνμλ€. λΉμ°ν κ²°κ³Όλ μκ°μ΄κ³Όμλ€!
κ°λ¨ν 쑰건 νλλ§ μΆκ°λ κ²μΈλ° ꡬνμ κ½€ μ λ₯Ό λ¨Ήμλ€.
λ¨ ν λ²μ bfs νΈμΆμ ν΅ν΄ λ²½μ λΆμλ κ²½μ°κΉμ§ μ²λ¦¬νλ λ°©μμ λΉκ΅μ κ°λ¨νλ€. λ¨μν λ²½μ λΆμ μ¬λΆλ₯Ό κΈ°λ‘νλ©΄ λλ€.
λ²½μ 1λ²μ΄λΌλ λΆμ κ²½μ°μλ μ΄ν λ²½μ λ§λ¬μ λ κ²½λ‘κ° λ§νκ² λλ€. νμ§λ§ λ²½μ ν λ²λ λΆμμ§ μμ κ²½μ°, μ΄ν λ²½μ λ§λ¬μ λ ν΄λΉ κ²½λ‘λ₯Ό λ«κ³ bfsλ₯Ό μ§ννλ©΄ λλ€!
κ·Έλ λ€λ©΄ κ·Έ μ½λλ₯Ό μλμμ νμΈν΄λ³΄μ!!
π©βπ» Code: μ€λ΅
import sys
from collections import deque
INF = sys.maxsize
def bfs():
global answer
q = deque([(1, 0, 0, False)])
visited = [[0]*m for _ in range(n)]
visited[0][0] = 1
while q:
dis, x, y, check = q.popleft()
if x == n-1 and y == m-1:
answer = min(dis, answer)
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0<=nx<n and 0<=ny<m:
if graph[nx][ny] == "0": # λ²½μ΄ μλ κ²½μ°
if visited[nx][ny] == 0:
visited[nx][ny] = 1
q.append((dis+1, nx, ny, check))
else: # λ²½μΌ κ²½μ°
if check == False:
q.append((dis+1, nx, ny, True))
return answer
n, m = map(int, sys.stdin.readline().split())
graph = [list(sys.stdin.readline().rstrip()) for _ in range(n)]
walls = []
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
answer = INF
bfs()
if answer == INF:
print(-1)
else:
print(answer)
νμ§λ§ ν΄λΉ μ½λλ μ λͺ©μμ μ μ μλ―μ΄ μ€λ΅μΈ μ½λμ΄λ€!
λλ체 μ????π«
μ΄λ λΆλΆμμ μμΈ μ²λ¦¬λ₯Ό νμ§ λͺ»νμκΉ κ³μ κ³ λ―Όνλ μ€, λ€λ₯Έ λΆμ΄ ν μ§λ¬Έ λλΆμ μμΈμ μ°Ύμ μ μμλ€..γ γ
π©βπ» Code1: μμΈ μΌμ΄μ€
μ μ½λλ λ€μμ μμΈ μΌμ΄μ€λ₯Ό κ°μ§λ€.
4 6
010100
010110
000110
111110
- (0,1)μμ λ²½μ λΆμκ³ κ° κ²½μ°
- (0, 2)λ λ°©λ¬Έμ²λ¦¬ λλ€.
- (0, 3)μμ λ€μ λ²½μ λ§λ κ²½λ‘λ₯Ό λ§λ¬΄λ¦¬νκ² λλ€.
- (0, 1)μμ λ²½μ λΆμμ§ μμ κ²½μ°
- Uμ ννλ‘ λκ³ λμ (0, 2)λ₯Ό λ°©λ¬Ένκ² λλ€.
- νμ§λ§ λ°©λ¬Έ μ²λ¦¬κ° μ΄λ―Έ λμ΄ μμΌλ―λ‘ κ²½λ‘λ₯Ό λ§λ¬΄λ¦¬νλ€.βοΈ
μ¦, λ²½μ λΆμ κ²½μ°μ λΆμμ§ μμ κ²½μ°λ₯Ό λΆλ¦¬ν΄ λ°©λ¬Έ κΈ°λ‘μ μ μ₯ν΄μΌ νλ€. κ·Έ μ΄μ λ λ¨μνλ€!
λ²½μ λΆμ κ²½μ°μ λΆμμ§ μμ κ²½μ°μ κ²½λ‘κ° λ€λ₯΄κΈ° λλ¬Έμ΄λ€.
- λ²½μ λΆμ μνμμ (0,2)μ λμ°©ν κ²½μ°, (0,3)μμ λ²½μ λ§λ κ²½λ‘λ₯Ό λ§λ¬΄λ¦¬νλ€.
- λ²½μ λΆμμ§ μμ μνμμ (0, 2)μ λμ°©ν κ²½μ°, (0, 3)μμ λ²½μ λ§λλλΌλ λ²½μ λΆμκ³ λμκ° μ μλ€.
μ΄μ μ΄λ₯Ό μ μ©ν μ½λλ₯Ό νμΈν΄λ³΄μ!
π©βπ» Code: μ λ΅
import sys
from collections import deque
INF = sys.maxsize
def bfs():
global answer
q = deque([(1, 0, 0, False)])
# λ°©λ¬Έ κΈ°λ‘μ λΆλ¦¬!
visited1 = [[0]*m for _ in range(n)]
visited2 = [[0]*m for _ in range(n)]
visited1[0][0] = 1
while q:
dis, x, y, check = q.popleft()
if x == n-1 and y == m-1:
return dis
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0<=nx<n and 0<=ny<m:
if graph[nx][ny] == "0": # λ²½μ΄ μλ κ²½μ°
if not check: # λ²½μ νλ¬Όμ§ μκ³ μ§λκ°λ κ²½μ°
if visited1[nx][ny] == 0:
visited1[nx][ny] = 1
q.append((dis+1, nx, ny, check))
else: # λ²½μ νλ¬Όκ³ μ§λκ°λ κ²½μ°
if visited2[nx][ny] == 0:
visited2[nx][ny] = 1
q.append((dis+1, nx, ny, check))
else: # λ²½μΌ κ²½μ°
if check == False:
q.append((dis+1, nx, ny, True))
return -1
n, m = map(int, sys.stdin.readline().split())
graph = [list(sys.stdin.readline().rstrip()) for _ in range(n)]
walls = []
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
print(bfs())
bfs λ¬Έμ λΌκ³ λ€ λκ°μ λ¬Έμ λ μλꡬλλ₯Ό νμ€ν λλ λ¬Έμ μλ€. λ€λ₯Έ μ‘°κ±΄μ΄ λΆμ bfs λ¬Έμ λ ν΄λΉ λ¬Έμ νμ΄λ₯Ό μ°Έκ³ ν΄ μ νμ΄λκ° μ μμ κ² κ°μ λλ~
λκΈλ¨κΈ°κΈ°