Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- [1,2,3],
- [4,8,2],
- [1,5,3],
- ]
- const minPath = (grid, x, y) => {
- const maxWidth = x;
- const maxHeight = y;
- let next = [{ x: 0, y: 0, weight: grid[0][0] }];
- let arr = next;
- do {
- arr = next;
- next = [];
- for (let i = 0; i < arr.length; ++i) {
- let node = arr[i];
- if (node.x < maxWidth) {
- next.push({
- x: node.x + 1,
- y: node.y,
- weight: node.weight + grid[node.y][node.x + 1]
- });
- }
- if (node.y < maxHeight) {
- next.push({
- x: node.x,
- y: node.y + 1,
- weight: node.weight + grid[node.y + 1][node.x]
- });
- }
- }
- } while (next.length);
- return Math.min(...arr.map(o => o.weight));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement