-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1953_탈주범검거.py
45 lines (39 loc) · 1.38 KB
/
1953_탈주범검거.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
45
# change = {0:(0,1), 1:(0,-1), 2:(1,0), 3:(-1,0)}
d = {1:[(1,0), (-1,0), (0,1), (0,-1)],
2:[(1,0), (-1,0)],
3:[(0,1), (0,-1)],
4:[(-1,0), (0,1)],
5:[(1,0), (0,1)],
6:[(1,0), (0,-1)],
7:[(-1,0), (0,-1)]
} # i,j 행,열 (세로, 가로)순서인거 주의
compliment = {(1,0):(-1,0), (-1,0):(1,0), (0,1):(0,-1), (0,-1):(0,1)} # 튜플은 딕셔너리의 키값으로 들어갈 수 있다
test_num = int(input())
for t in range(test_num):
result = 0
N, M, R, C, L = map(int,input().split())
board = [0]*N
for i in range(N):
board[i] = list(map(int,input().split()))
visited = [[False for j in range(M)] for i in range(N)]
time = 1
cnt = 1
queue = [(R,C)]
visited[R][C] = True
while queue:
if time == L:
break
else:
time += 1
for __ in range(len(queue)):
i, j = queue.pop(0)
for di, dj in d[board[i][j]]:
ri = i + di
rj = j + dj
if 0 <= ri < N and 0 <= rj < M and board[ri][rj] != 0 and visited[ri][rj] == False:
if compliment[(di,dj)] in d[board[ri][rj]]:
queue.append((ri,rj))
visited[ri][rj] = True
cnt += 1
print('#' + str(t+1) + ' ', end='')
print(cnt)