-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmazeShortestPath.js
More file actions
31 lines (23 loc) · 905 Bytes
/
mazeShortestPath.js
File metadata and controls
31 lines (23 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var weight = [[1,1,1],[10,5,1],[1,8, 1], [10,11,1]];
function shortestPath(xStart, yStart, xFinal, yFinal, weight) {
if(xStart > xFinal) return 1000;
if(yStart > yFinal) return 1000;
if(xStart === xFinal && yStart == yFinal) {
return weight[xStart][yStart];
}
var optionA = shortestPath(xStart + 1, yStart, xFinal, yFinal, weight);
var optionB = shortestPath(xStart, yStart + 1, xFinal, yFinal, weight);
var totalWeight = weight[xStart][yStart];
var src = "(" + xStart + "," + yStart + ")";
var dest = "";
if(optionA < optionB) {
totalWeight += optionA;
dest = "(" + (xStart + 1) + ", " + yStart + ")";
} else {
totalWeight += optionB;
dest = "(" + xStart + ", " + (yStart + 1) + ")";
}
console.log(src + "->" + dest + " w:" + totalWeight);
return totalWeight;
}
shortestPath(0,0,3,2, weight);