๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon]python #2178 ๋ฏธ๋กœํƒ์ƒ‰

by DevIseo 2023. 1. 25.

[baekjoon]python #2178 ๋ฏธ๋กœํƒ์ƒ‰

 

https://www.acmicpc.net/problem/2178

 

2178๋ฒˆ: ๋ฏธ๋กœ ํƒ์ƒ‰

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ N, M(2 ≤ N, M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” M๊ฐœ์˜ ์ •์ˆ˜๋กœ ๋ฏธ๋กœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ์ˆ˜๋“ค์€ ๋ถ™์–ด์„œ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

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

n,m = map(int,input().split())
arr = [list(map(int,input().strip())) for _ in range(n)]
visit = [[0]*m for _ in range(n)]

queue = deque()
visit[0][0] = 1
queue.append((0,0))

while queue:
    ny,nx = queue.popleft()
    arr[ny][nx] = 0

    if ny == n and nx == m:
        break

    directy = [-1,1,0,0]
    directx = [0,0,-1,1]

    for i in range(4):
        dy = directy[i] + ny
        dx = directx[i] + nx

        if 0<=dy<n and 0<=dx<m:
            if arr[dy][dx] == 1 and visit[dy][dx] == 0:
                visit[dy][dx]=visit[ny][nx]+1
                queue.append((dy,dx))

print(visit[-1][-1])

๋Œ“๊ธ€