Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. n, m = [int(i) for i in input().split()]
  2. p = [[int(i) for i in input().split()] for j in range(n)]
  3. bfs = [[1000000000 for i in range(m)] for j in range(n)]
  4. bfs[0][0] = p[0][0]
  5.  
  6. queue = [[0, 0]]
  7. while(len(queue) > 0):
  8. a = queue[0]
  9. x = a[1]
  10. y = a[0]
  11. del queue[0]
  12. if(y > 0):
  13. if(bfs[y - 1][x] > bfs[y][x] + p[y - 1][x]):
  14. queue.append([y - 1, x])
  15. bfs[y - 1][x] = bfs[y][x] + p[y - 1][x]
  16. if(y < n - 1):
  17. if(bfs[y + 1][x] > bfs[y][x] + p[y + 1][x]):
  18. queue.append([y + 1, x])
  19. bfs[y + 1][x] = bfs[y][x] + p[y + 1][x]
  20. if(x > 0):
  21. if(bfs[y][x - 1] > bfs[y][x] + p[y][x - 1]):
  22. queue.append([y, x - 1])
  23. bfs[y][x - 1] = bfs[y][x] + p[y][x - 1]
  24. if(x < m - 1):
  25. if(bfs[y][x + 1] > bfs[y][x] + p[y][x + 1]):
  26. queue.append([y, x + 1])
  27. bfs[y][x + 1] = bfs[y][x] + p[y][x + 1]
  28. print(bfs[n - 1][m - 1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement