Skip to content

Commit

Permalink
08.19: bfs(점프 게임)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Aug 19, 2024
1 parent 40186a3 commit 18787cc
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions baekjoon/15558.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 점프 게임 : 그래프, 너비 우선 탐색
const input = require("fs")
.readFileSync(0, "utf-8")
.toString()
.trim()
.split("\n");
const [n, k] = input[0].split(" ").map((v) => Number(v));
const leftLine = input[1].split("").map((v) => Number(v));
const rightLine = input[2].split("").map((v) => Number(v));
const map = [leftLine, rightLine];
const [LEFT, RIGHT] = [0, 1];

const visited = [leftLine.slice().fill(false), rightLine.slice().fill(false)];
const q = [[LEFT, 0, 0]];
let idx = 0;
let isDone = false;

while (idx < q.length) {
const [currDir, currNum, currTime] = q[idx++];
const next = [
[currDir, currNum + 1, currTime + 1],
[currDir, currNum - 1, currTime + 1],
[Number(!currDir), currNum + k, currTime + 1],
];

for (const [nextDir, nextNum, nextTime] of next) {
if (nextNum > n - 1) {
isDone = true;
break;
}
if (nextNum === n - 1) {
if (map[nextDir][nextNum] === 1) {
isDone = true;
break;
}
}
if (visited[nextDir][nextNum] || map[nextDir][nextNum] === 0) {
continue;
}
if (nextTime - 1 >= nextNum) {
continue;
}
visited[nextDir][nextNum] = true;
q.push([nextDir, nextNum, nextTime]);
}
if (isDone) {
break;
}
}

console.log(isDone ? 1 : 0);

0 comments on commit 18787cc

Please sign in to comment.