-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrobotPathsRedux.js
More file actions
32 lines (28 loc) · 788 Bytes
/
robotPathsRedux.js
File metadata and controls
32 lines (28 loc) · 788 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
31
32
var range = function (n, elm) {
var out = [];
for (var i = 0; i < n; i++) {
if (Array.isArray(elm)) {
out.push(elm.slice());
} else {
out.push(elm);
}
}
return out;
};
var outOfBounds = function(n, row, col) {
return n > row && n > col && row > 0 && col > 0;
};
var robotPathsRedux = function (board, n, row, col) {
if (row === n - 1 && col === n - 1) {
return 1;
}
if (board[row][col] || outOfBounds(n, row, col)) {
return 0;
}
board[row][col] = true;
return robotPathsRedux(board.slice(), row + 1, col) +
robotPathsRedux(board.slice(), row - 1, col) +
robotPathsRedux(board.slice(), row, col + 1) +
robotPathsRedux(board.slice(), row, col - 1);
};
robotPathsRedux(range(5, range(5, false)), 5, 0, 0);