-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-2d-path.py
48 lines (35 loc) · 1.02 KB
/
dynamic-2d-path.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
from functools import cache
from typing import Tuple
def main():
n, m = (int(v) for v in input().split())
table = [[int(v) for v in input().split()] for _ in range(n)]
assert all(len(row) == m for row in table)
def neighbors(x, y):
if y < n:
yield "D", (x, y + 1)
if x < m:
yield "R", (x + 1, y)
def value_at(x, y):
return table[y - 1][x - 1]
initial = 1, 1
final = m, n
@cache
def answer(p) -> Tuple[int, list]:
def choices():
# Ascend
if p == final:
yield value_at(*p), []
# Go D or R
for dir, np in neighbors(*p):
pvalue, path = answer(np)
yield value_at(*p) + pvalue, (dir, path)
return max(choices())
def gen_path(path):
while path:
dir, path = path
yield dir
best_value, best_path = answer(initial)
print(best_value)
print(*gen_path(best_path))
if __name__ == "__main__":
main()