-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
|
||
function solution() { | ||
let answer = []; | ||
let queue = []; | ||
queue.push(1); // 루트 노드 먼저 넣어놓고 시작하기 | ||
|
||
while (queue.length) { | ||
let v = queue.shift(); // 맨 앞 노드 하나 빼기 (빼는 순간 방문했다고 침) | ||
answer.push(v); | ||
|
||
for (let nextVertex of [v * 2, v * 2 + 1]) { | ||
if (nextVertex > 7) continue; // 7보다 크면 건너뜀 | ||
queue.push(nextVertex); // v의 자식 노드들(v*2, v*2+1)을 queue에 넣어줌 | ||
} | ||
} | ||
return answer; | ||
} | ||
|
||
console.log(solution()); | ||
|
||
// 주로 상태트리 문제 (ex. 출발에서 도착지점으로 갈 때의 최단 거리를 구해라) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"use strict"; | ||
|
||
function solution(n, board) { | ||
let answer = 0; | ||
let dx = [-1, -1, 0, 1, 1, 1, 0, -1]; | ||
let dy = [0, 1, 1, 1, 0, -1, -1, -1]; | ||
|
||
function dfs(x, y) { | ||
board[x][y] = 0; | ||
|
||
for (let k = 0; k < 8; k++) { | ||
let nx = x + dx[k]; | ||
let ny = y + dy[k]; | ||
|
||
if (nx >= 0 && nx < n && ny >= 0 && ny < n && board[nx][ny] === 1) { | ||
console.log(nx, ny); | ||
dfs(nx, ny); | ||
} | ||
} | ||
} | ||
|
||
for (let i = 0; i < n; i++) { | ||
for (let j = 0; j < n; j++) { | ||
if (board[i][j] === 1) { | ||
answer++; | ||
console.log(i, j); | ||
dfs(i, j); | ||
console.log("dfs end"); | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
|
||
let arr = [ | ||
[1, 1, 0, 0, 0, 1, 0], | ||
[0, 1, 1, 0, 1, 1, 0], | ||
[0, 1, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 1, 0, 1, 1], | ||
[1, 1, 0, 1, 1, 0, 0], | ||
[1, 0, 0, 0, 1, 0, 0], | ||
[1, 0, 1, 0, 1, 0, 0], | ||
]; | ||
|
||
console.log(solution(7, arr)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters