728x90
๋ฌธ์
ํ์ด
n, m, k = map(int, input().split())
graph = [input().rstrip() for _ in range(n)]
visited = [[k + 1 for _ in range(m)] for _ in range(n)]
visited[0][0] = 0
visited๋ ๋์ฐฉํ์ ๋ ๋ฒฝ์ ๋ถ์ ํ์๋ฅผ ๋ฃ๋๋ค.
์ด๊ธฐ ๊ฐ์ผ๋ก ๋ฒฝ์ ์ต๋๋ก ๋ถ์ ์ ์๋ ํ์๋ฅผ ๋ฃ๋๋ค.
๋ง์ฝ (x, y) ์ขํ์์ ๋ฒฝ์ ๋ถ์ ํ์(w)๊ฐ 5๋ฒ์ด๊ณ , k+1 = 7์ด๋ผ๊ณ ๊ฐ์ ํ์ ๋
visited[y][x] = 4 ์ธ ๊ฒฝ์ฐ (x, y) ์ขํ์ ๋ฒฝ์ 4๋ฒ ๋ถ์๊ณ ๋ฐฉ๋ฌธํ๋ค๋ ๋ป
visited[y][x] = 7์ธ ๊ฒฝ์ฐ (x, y) ์ขํ๋ฅผ ์์ง ๋ฐฉ๋ฌธํ์ง ์์๋ค๋ ๋ป
์ฝ๋
from collections import deque
import sys
input = sys.stdin.readline
def bfs(start):
q = deque()
q.append(start)
ans = 1
time = True
while q:
for _ in range(len(q)):
i, j, w = q.popleft()
if i == n - 1 and j == m - 1:
print(ans)
return
for dy, dx in dir:
ni, nj = i + dy, j + dx
if ni < 0 or ni >= n or nj < 0 or nj >= m or visited[ni][nj] <= w:
continue
#๋ฒฝ์ด ์๋ ๊ฒฝ์ฐ ๋ฎ์ด๋ ๋ฐค์ด๋ ์ด๋ ๊ฐ๋ฅ
if graph[ni][nj] == '0':
q.append((ni, nj, w))
visited[ni][nj] = w
#๋ฒฝ์ธ ๊ฒฝ์ฐ
elif w < k:
if not time: #๋ฐค ์ธ ๊ฒฝ์ฐ
q.append((i, j, w))
else:
visited[ni][nj] = w
q.append((ni, nj, w + 1))
ans += 1
time = not time
print(-1)
return
n, m, k = map(int, input().split())
graph = [input().rstrip() for _ in range(n)]
visited = [[k + 1 for _ in range(m)] for _ in range(n)]
visited[0][0] = 0
dir = ((1, 0), (-1, 0), (0, 1), (0, -1))
bfs((0,0,0))
๋ง๋ฌด๋ฆฌ
ํ๋ค๊ฐ ๋ชจ๋ฅด๊ฒ ์ด์ ๊ตฌ๊ธ๋ง ํ๋๋ฐ... ์๊ฐ๋ณด๋ค ์ฌ์ด ๋ฌธ์ ์ฌ์ ํํํ๋ค.
๋๋ฌด ๊น๊ฒ ์๊ฐํ๋ ์ถ๊ธฐ๋ ํ๊ณ , ๊ตฌ์ฒด์ ์ธ ๋ถ๋ถ์ ๋ง์ด ๋์น ๊ฒ ๊ฐ๊ธฐ๋ ํ๋ค.
์์ง ๊ณจ๋ 1์ ๋์ด๋๋ ์กฐ๊ธ ๋ฌด๋ฆฌ์ธ ๊ฒ ๊ฐ๋ค...
728x90