2020/06/24

[zerojudge] e287: 機器人的路徑

import sys

n, m = [int(x) for x in sys.stdin.readline().split()]
f = []
for _ in range(n): f += [int(x) for x in sys.stdin.readline().split()]
v = f.index(min(f))
y, x, r = v // m, v % m, 0
while(1):
    ind = y * m + x
    r += f[ind]
    f[ind] = -1
    c = [[y, x+1], [y+1, x], [y, x-1], [y-1, x]]
    c = [z for z in c if z[0] >= 0 and z[0] < n and z[1] >= 0 and z[1] < m]
    if(not c): break
    c = [(z[0], z[1], f[z[0] * m + z[1]]) for z in c]
    c = [z for z in c if z[2] >= 0]
    if(not c): break
    c.sort(key = lambda z: z[2])
    y, x, _ = c[0]
        
print(r)

沒有留言:

張貼留言