Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n, m = [int(i) for i in input().split()]
- p = [[int(i) for i in input().split()] for j in range(n)]
- bfs = [[1000000000 for i in range(m)] for j in range(n)]
- bfs[0][0] = p[0][0]
- queue = [[0, 0]]
- while(len(queue) > 0):
- a = queue[0]
- x = a[1]
- y = a[0]
- del queue[0]
- if(y > 0):
- if(bfs[y - 1][x] > bfs[y][x] + p[y - 1][x]):
- queue.append([y - 1, x])
- bfs[y - 1][x] = bfs[y][x] + p[y - 1][x]
- if(y < n - 1):
- if(bfs[y + 1][x] > bfs[y][x] + p[y + 1][x]):
- queue.append([y + 1, x])
- bfs[y + 1][x] = bfs[y][x] + p[y + 1][x]
- if(x > 0):
- if(bfs[y][x - 1] > bfs[y][x] + p[y][x - 1]):
- queue.append([y, x - 1])
- bfs[y][x - 1] = bfs[y][x] + p[y][x - 1]
- if(x < m - 1):
- if(bfs[y][x + 1] > bfs[y][x] + p[y][x + 1]):
- queue.append([y, x + 1])
- bfs[y][x + 1] = bfs[y][x] + p[y][x + 1]
- print(bfs[n - 1][m - 1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement