어렵다..
board
에 왼쪽 상단에 로봇이 위치한다어떻게 풀어야하는지 도무지 감이 오지않아 구글에 검색해보았다. 검색하여 알아낸 코드는 아래와 같다
function makeBoard(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) {
this[i][j] = !this[i][j];
};
board.hasBeenVisited = function(i, j) {
return !!this[i][j];
};
return board;
}
function robotPaths(n) {
var pathCount = 0;
var board = makeBoard(n);
function pathCounter(x, y) {
// 경우의수 카운팅
if (x === n - 1 && y === n - 1) {
pathCount++;
return;
}
if (x < 0 || y < 0 || x >= n || y >= n) {
// 길이 없음
return;
}
if (board.hasBeenVisited(x, y)) {
// 방문했던 지점
return;
}
board.togglePiece(x, y);
console.log("board ? ", board);
pathCounter(x, y + 1); // right
pathCounter(x + 1, y); // bottom
pathCounter(x, y - 1); // left
pathCounter(x - 1, y); // top
board.togglePiece(x, y);
console.log("board ? ", board);
}
pathCounter(0, 0);
return pathCount;
}
robotPaths(2);
https://repl.it/@jordanbishop/SOLVEDrobotPaths
http://rvbsanjose.github.io/robot-paths/
https://www.youtube.com/watch?v=qJPYTIojBs0