Skip to content

Commit

Permalink
Implement robotPaths.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 10, 2015
1 parent 8eb1076 commit 063a494
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions robotPaths/robotPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,58 @@

// A Board class will be useful

var makeBoard = function(n) {
var makeBoard = function (n) {
var board = [];

for (var i = 0; i < n; i++) {
board.push([]);
for (var j = 0; j < n; j++) {
board[i].push(false);
}
}
board.togglePiece = function(i, j) {

board.togglePiece = function (i, j) {
this[i][j] = !this[i][j];
}
board.hasBeenVisited = function(i, j) {
};

board.hasBeenVisited = function (i, j) {
return !!this[i][j];
}
};

board.withInBoard = function (i, j) {
return (i >= 0 && i < n && j >= 0 && j < n);
};

return board;
};

var robotPaths = function(n) {
// YOUR CODE HERE
}
var robotPaths = function (n) {
var total = 0;
var board = makeBoard(n);
var inner = function (board, x, y) {
// base case
if (x === n - 1 && y === n - 1 && !board.hasBeenVisited(x, y)) {
total += 1;
return;
}

if (board.withInBoard(x, y) && !board.hasBeenVisited(x, y)) {
board.togglePiece(x, y);

// check left position
inner(board, x - 1, y);
// check right position
inner(board, x + 1, y);
// check top position
inner(board, x, y - 1);
// check bottom position
inner(board, x, y + 1);

// untoggle current position
board.togglePiece(x, y);
}
};

inner(board, 0, 0);
return total;
};

0 comments on commit 063a494

Please sign in to comment.