πŸ’‘λ¬Έμ œ 뢄석 μš”μ•½

πŸ’‘μ•Œκ³ λ¦¬μ¦˜ 섀계

πŸ’‘μ½”λ“œ

const fs = require('fs');
const path = require('path');

const inputPath = path.join(__dirname, 'input.txt');
const input = fs.readFileSync(inputPath, 'utf-8').trim().split(' ');

// μž…λ ₯κ°’ νŒŒμ‹±
const [N, K] = input.map(Number);

// 값이 μ˜¬λ°”λ₯΄κ²Œ νŒŒμ‹±λ˜μ—ˆλŠ”μ§€ 확인
if (isNaN(N) || isNaN(K)) {
  console.error("μž…λ ₯ 값이 잘λͺ»λ˜μ—ˆμŠ΅λ‹ˆλ‹€. Nκ³Ό Kλ₯Ό μ˜¬λ°”λ₯΄κ²Œ μž…λ ₯ν•˜μ„Έμš”.");
  process.exit(1);
}

function hideAndSeek(N, K) {
  if (N === K) return 0; // 좜발점과 도착점이 κ°™μœΌλ©΄ 0초
  
  const visited = Array(100001).fill(false); // λ°©λ¬Έ μ—¬λΆ€ 체크 λ°°μ—΄
  const queue = [[N, 0]]; // [ν˜„μž¬ μœ„μΉ˜, κ±Έλ¦° μ‹œκ°„]을 μ €μž₯ν•˜λŠ” 큐
  visited[N] = true; // 좜발점 방문 처리
  
  while (queue.length > 0) {
    const [current, time] = queue.shift();
    
    // μ„Έ κ°€μ§€ 경우λ₯Ό 탐색
    for (let next of [current - 1, current + 1, current * 2]) {
      if (next === K) return time + 1; // λͺ©ν‘œ 지점에 λ„λ‹¬ν•˜λ©΄ μ‹œκ°„ λ°˜ν™˜
      
      // λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜μ§€ μ•Šκ³  λ°©λ¬Έν•˜μ§€ μ•Šμ€ μœ„μΉ˜μΌ 경우 큐에 μΆ”κ°€
      if (next >= 0 && next <= 100000 && !visited[next]) {
        visited[next] = true; // 방문 처리
        queue.push([next, time + 1]); // 큐에 [μœ„μΉ˜, μ‹œκ°„] μΆ”κ°€
      }
    }
  }
}

console.log(hideAndSeek(N, K)); // κ²°κ³Ό 좜λ ₯

πŸ’‘μ‹œκ°„λ³΅μž‘λ„