forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.py
44 lines (36 loc) ยท 1.46 KB
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from collections import deque
n, k = map(int, input().split())
graph = [] # ์ ์ฒด ๋ณด๋ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฆฌ์คํธ
data = [] # ๋ฐ์ด๋ฌ์ค์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฆฌ์คํธ
for i in range(n):
# ๋ณด๋ ์ ๋ณด๋ฅผ ํ ์ค ๋จ์๋ก ์
๋ ฅ
graph.append(list(map(int, input().split())))
for j in range(n):
# ํด๋น ์์น์ ๋ฐ์ด๋ฌ์ค๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ
if graph[i][j] != 0:
# (๋ฐ์ด๋ฌ์ค ์ข
๋ฅ, ์๊ฐ, ์์น X, ์์น Y) ์ฝ์
data.append((graph[i][j], 0, i, j))
# ์ ๋ ฌ ์ดํ์ ํ๋ก ์ฎ๊ธฐ๊ธฐ (๋ฎ์ ๋ฒํธ์ ๋ฐ์ด๋ฌ์ค๊ฐ ๋จผ์ ์ฆ์ํ๋ฏ๋ก)
data.sort()
q = deque(data)
target_s, target_x, target_y = map(int, input().split())
# ๋ฐ์ด๋ฌ์ค๊ฐ ํผ์ ธ๋๊ฐ ์ ์๋ 4๊ฐ์ง์ ์์น
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
# ๋๋น ์ฐ์ ํ์(BFS) ์งํ
while q:
virus, s, x, y = q.popleft()
# ์ ํํ s์ด๊ฐ ์ง๋๊ฑฐ๋, ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
if s == target_s:
break
# ํ์ฌ ๋
ธ๋์์ ์ฃผ๋ณ 4๊ฐ์ง ์์น๋ฅผ ๊ฐ๊ฐ ํ์ธ
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# ํด๋น ์์น๋ก ์ด๋ํ ์ ์๋ ๊ฒฝ์ฐ
if 0 <= nx and nx < n and 0 <= ny and ny < n:
# ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์์น๋ผ๋ฉด, ๊ทธ ์์น์ ๋ฐ์ด๋ฌ์ค ๋ฃ๊ธฐ
if graph[nx][ny] == 0:
graph[nx][ny] = virus
q.append((virus, s + 1, nx, ny))
print(graph[target_x - 1][target_y - 1])