Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. [
  2. [1,2,3],
  3. [4,8,2],
  4. [1,5,3],
  5. ]
  6.  
  7. const minPath = (grid, x, y) => {
  8. const maxWidth = x;
  9. const maxHeight = y;
  10.  
  11. let next = [{ x: 0, y: 0, weight: grid[0][0] }];
  12. let arr = next;
  13.  
  14. do {
  15. arr = next;
  16. next = [];
  17.  
  18. for (let i = 0; i < arr.length; ++i) {
  19. let node = arr[i];
  20.  
  21. if (node.x < maxWidth) {
  22. next.push({
  23. x: node.x + 1,
  24. y: node.y,
  25. weight: node.weight + grid[node.y][node.x + 1]
  26. });
  27. }
  28.  
  29. if (node.y < maxHeight) {
  30. next.push({
  31. x: node.x,
  32. y: node.y + 1,
  33. weight: node.weight + grid[node.y + 1][node.x]
  34. });
  35. }
  36. }
  37. } while (next.length);
  38.  
  39. return Math.min(...arr.map(o => o.weight));
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement