๐ ์ฒ์์ ์์ ์ง์ ๊ณผ ๋ชฉํ ์ง์ ์ด ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํด์ฃผ์ง ์์ ํ๋ ธ์๋ค.
์ฝ๋
const fs = require('fs');
const [F, S, G, U, D] = fs.readFileSync('/dev/stdin').toString().trim().split(" ").map(Number);
const willCheck = [[S, 0]];
const check = new Array(F + 1).fill(0);
check[S] = 1;
//์์ ์ง์ ๊ณผ ๋ชฉํ ์ง์ ์ด ๊ฐ์ ๊ฒฝ์ฐ
if (S === G) {
return console.log(0);
}
while (willCheck.length) {
const [pos, count] = willCheck.shift();
const nextUp = pos + U;
const nextDown = pos - D;
if (nextUp <= F+1) {
if (nextUp === G) {
return console.log(count + 1);
} else {
if (check[nextUp] === 0) {
check[nextUp] = 1;
willCheck.push([nextUp, count + 1]);
}
}
}
if (nextDown > 0) {
if (nextDown === G) {
return console.log(count + 1);
} else {
if (check[nextDown] === 0) {
check[nextDown] = 1;
willCheck.push([nextDown, count + 1]);
}
}
}
}
console.log('use the stairs')