Skip to content

Commit

Permalink
#9 22.11.21 > 미로 탐색(DFS)
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Nov 21, 2022
1 parent 602ac32 commit b6c536a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/inf/dfs,bfs/4_rere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

function solution(board) {
let answer = 0;
let dx = [-1, 0, 1, 0];
let dy = [0, 1, 0, -1];

function dfs(x, y) {
if (x === 6 && y === 6) {
answer++;
} else {
for (let i = 0; i < 4; i++) {
let nx = x + dx[i];
let ny = y + dy[i];

if (nx >= 0 && nx <= 6 && ny >= 0 && ny <= 6 && board[nx][ny] === 0) {
board[nx][ny] = 1;
dfs(nx, ny);
board[nx][ny] = 0; // back했으니
}
}
}
}
board[0][0] = 1;
dfs(0, 0);
return answer;
}

let arr = [
[0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 1, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 1],
[1, 1, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
];

console.log(solution(arr));
2 changes: 1 addition & 1 deletion src/inf/dfs,bfs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| :--: | :------: | :--------: |
| 2 | X | 22.11.20😢 |
| 3 | X | 22.11.20 |
| 4 | X | O |
| 4 | X | 22.11.21 |
| 5 | X | O |
| 6 | X | O |
| 7-1 || O |
Expand Down

0 comments on commit b6c536a

Please sign in to comment.