๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆซ์ž ๋ณ€ํ™˜ํ•˜๊ธฐ JS

by megan07 2024. 1. 11.

 

ํ’€์ด๋ฐฉ๋ฒ•!

bfs๋กœ ๋ฐฐ์—ด๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค
x์—์„œ๋ถ€ํ„ฐ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ
ํšŸ์ˆ˜๊ฐ€ ์ ์œผ๋ฉด table์„ ๊ฐฑ์‹ ํ•˜๊ณ  ํ์— ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋ฐฐ์—ด๊ฐ’์„ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ true,false๋กœ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋งŒ ๊ฐฑ์‹ ํ•ด๋„ ์ƒ๊ด€์—†๋Š”
์ „ํ˜•์ ์ธ bfs ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค

๊ทธ๋ฆฌ๊ณ  ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์˜ shift๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์ง์ ‘ ํ๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค



์˜ˆ์ „์— ๋ฐฑ์ค€์—์„œ
https://www.acmicpc.net/problem/13549

์ˆจ๋ฐ”๊ผญ์งˆ 3 ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ

์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ๋Š” ์ด๋™ํ•  ๋•Œ 0์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ

์ตœ์†Œ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค

๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ์ž˜ ์ƒ๊ฐํ•˜๋ฉด์„œ ํ’€์–ด์•ผ๊ฒ ๋„ค์š”

 

 

์ตœ์†Œ๊ฐ’ ๊ฐฑ์‹ 

function solution(x, y, n) {
  //bfs

  //๊ฐ ์ˆซ์ž์— ๋„๋‹ฌํ•˜๋Š” ์ตœ์†Œ ์—ฐ์‚ฐ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ํ…Œ์ด๋ธ”
  const table = new Array(y + 1).fill(Infinity);
  const q = new Queue();
  const calc = [
    ['+', n],
    ['*', 2],
    ['*', 3],
  ];

  q.enqueue({ val: x, cnt: 0 });
  table[x] = 0;

  //ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€
  while (q.size) {
    const current = q.dequeue();

    //3๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณต(+n, *2, *3)
    for (let i = 0; i < 3; i++) {
      const next = calcNextVal(current.val, calc[i][0], calc[i][1]);

      //์—ฐ์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ y์ดํ•˜์ด๊ณ  ๋” ์ ์€ ํšŸ์ˆ˜๋กœ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•˜๋ฉด table ๊ฐฑ์‹ ํ•˜๊ณ  ํ์— ๋„ฃ์–ด์คŒ
      if (next <= y && table[next] > current.cnt + 1) {
        table[next] = current.cnt + 1;
        q.enqueue({ val: next, cnt: current.cnt + 1 });
      }
    }
  }

  function calcNextVal(currentValue, operator, num) {
    if (operator === '+') return currentValue + num;
    return currentValue * num;
  }

  return table[y] !== Infinity ? table[y] : -1;
}

 

 

 

visited๋กœ ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋งŒ ํŒ๋‹จ


function solution(x, y, n) {
  //bfs

  //๊ฐ ์ˆซ์ž์— ๋„๋‹ฌํ•˜๋Š” ์ตœ์†Œ ์—ฐ์‚ฐ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ํ…Œ์ด๋ธ”
  const visited = new Array(y + 1).fill(false);
  const q = new Queue();
  const calc = [
    ['+', n],
    ['*', 2],
    ['*', 3],
  ];

  q.enqueue({ val: x, cnt: 0 });
  visited[x] = true;

  //ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€
  while (q.size) {
    const current = q.dequeue();
      
      if(current.val === y){
          return current.cnt;
      }

    //3๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณต(+n, *2, *3)
    for (let i = 0; i < 3; i++) {
      const next = calcNextVal(current.val, calc[i][0], calc[i][1]);

      //์—ฐ์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ y์ดํ•˜์ด๊ณ  ๋ฐฉ๋ฌธ ์•ˆํ–ˆ์œผ๋ฉด ํ์— ๋„ฃ์–ด์คŒ
      if (next <= y && !visited[next]) {
        visited[next] = true;
        q.enqueue({ val: next, cnt: current.cnt + 1 });
      }
    }
  }

  function calcNextVal(currentValue, operator, num) {
    if (operator === '+') return currentValue + num;
    return currentValue * num;
  }

  return -1;
}

 

 

 

ํ ๊ตฌํ˜„ ์ฝ”๋“œ

class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
    this.size = 0;
  }

  enqueue(val) {
    const node = new Node(val);
    if (!this.front) {
      this.front = node;
      this.rear = node;
    } else {
      this.rear.next = node;
      this.rear = node;
    }

    return ++this.size;
  }

  dequeue() {
    if (!this.front) return null;

    const node = this.front;
    if (this.front === this.rear) {
      this.rear = null;
    }
    this.front = this.front.next;
    this.size--;

    return node.val;
  }
}