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 | def bfs():
global n, m, r, c, l, answer, time
check[r][c] = True
answer += 1
time += 1
queue = [(r, c, time)]
while queue:
curr = queue.pop(0)
curr_time = curr[2]
if curr_time == l:
break
for diff in dic[MAP[curr[0]][curr[1]]]:
new_y = curr[0]+diff[0]
new_x = curr[1]+diff[1]
new_time = curr[2]+1
if 0<=new_y<n and 0<=new_x<m and MAP[new_y][new_x] != 0:
if isConnectable(curr[0], curr[1], new_y, new_x):
if not check[new_y][new_x]:
queue.append((new_y, new_x, new_time))
check[new_y][new_x] = True
answer += 1
def isConnectable(curr_y, curr_x, next_y, next_x):
for d in dic[MAP[next_y][next_x]]:
prev_y = next_y + d[0]
prev_x = next_x + d[1]
if prev_y == curr_y and prev_x == curr_x:
return True
return False
dic = {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))}
for t in range(int(input())):
n, m, r, c, l = map(int, input().split())
MAP = [list(map(int, input().split())) for _ in range(n)]
check = [[False]*m for _ in range(n)]
answer = 0
time = 0
bfs()
print("#{} {}".format(t+1, answer))
|
댓글
댓글 쓰기