-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17070.py
77 lines (63 loc) · 2.16 KB
/
17070.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys
def pipe_move3(col, row, count):
move_right(col, row, count)
move_diagonal(col, row, count)
def pipe_move4(col, row, count):
move_right(col, row, count)
move_diagonal(col, row, count)
move_down(col, row, count)
def pipe_move6(col, row, count):
move_diagonal(col, row, count)
move_down(col, row, count)
def move_down(col, row, count):
global result
if col + 1 < N and not matrix[col + 1][row]:
if col + 1 == N - 1 and row == N - 1:
result += count
elif col + 1 < N - 1:
if (col + 1, row, 6) in dp[-1]:
dp[-1][(col + 1, row, 6)] += count
else:
dp[-1][(col + 1, row, 6)] = count
else:
pass
def move_diagonal(col, row, count):
global result
if col + 1 < N and row + 1 < N and not (matrix[col + 1][row + 1] or matrix[col + 1][row] or matrix[col][row + 1]):
if col + 1 == N - 1 and row + 1 == N - 1:
result += count
elif col + 1 <= N - 1 and row + 1 <= N - 1:
if (col + 1, row + 1, 4) in dp[-1]:
dp[-1][(col + 1, row + 1, 4)] += count
else:
dp[-1][(col + 1, row + 1, 4)] = count
else:
pass
def move_right(col, row, count):
global result
if row + 1 < N and not matrix[col][row + 1]:
if row + 1 == N - 1 and col == N - 1:
result += count
elif row + 1 < N - 1:
if (col, row + 1, 3) in dp[-1]:
dp[-1][(col, row + 1, 3)] += count
else:
dp[-1][(col, row + 1, 3)] = count
else:
pass
N = int(sys.stdin.readline())
matrix = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
dp = [{(0, 1, 3): 1}]
result = 0
while True:
dp.append(dict())
for item in dp[-2]:
if item[2] == 3:
pipe_move3(item[0], item[1], dp[-2][item])
elif item[2] == 6:
pipe_move6(item[0], item[1], dp[-2][item])
else:
pipe_move4(item[0], item[1], dp[-2][item])
if len(dp[-1]) == 0:
break
print(result)