Skip to content

Commit

Permalink
19-숨바꼭질-3
Browse files Browse the repository at this point in the history
  • Loading branch information
minbr0ther committed Apr 7, 2022
1 parent d236179 commit 1a37703
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2022/graph-traversal/19~24/19-숨바꼭질-3/app.js
@@ -0,0 +1,29 @@
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(filePath).toString().trim().split('\n');

const [start, finish] = input.shift().split(' ').map(Number);

const position = Array.from({ length: 100_001 }, () => 0);
const queue = [start];

while (queue.length) {
const x = queue.shift();

if (x === finish) {
console.log(position[x]);
break;
}

for (const nx of [x * 2, x - 1, x + 1]) {
if (!position[nx] && nx >= 0 && nx <= 100_000) {
if (nx === x * 2) {
position[nx] = position[x];
queue.unshift(nx);
} else {
position[nx] = position[x] + 1;
queue.push(nx);
}
}
}
}
1 change: 1 addition & 0 deletions 2022/graph-traversal/19~24/19-숨바꼭질-3/input.txt
@@ -0,0 +1 @@
5 17

0 comments on commit 1a37703

Please sign in to comment.