๐ŸŽฒ ๋ฐฑ์ค€ 5014๋ฒˆ ์Šคํƒ€ํŠธ๋งํฌ

Jeongeunยท2023๋…„ 7์›” 9์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
87/187

๋ฐฑ์ค€ 5014๋ฒˆ

๐Ÿ’Š ์ฒ˜์Œ์— ์‹œ์ž‘ ์ง€์ ๊ณผ ๋ชฉํ‘œ ์ง€์ ์ด ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ์ง€ ์•Š์•„ ํ‹€๋ ธ์—ˆ๋‹ค.

์ฝ”๋“œ

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')

0๊ฐœ์˜ ๋Œ“๊ธ€