ํ์ด๋ฐฉ๋ฒ!
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;
}
}
'๐ง ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] ๋๋ก์ ๊ฐ์ 1577 JS (0) | 2024.01.29 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ง๋ฒ์ ์๋ฆฌ๋ฒ ์ดํฐ JS (0) | 2024.01.12 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๋ฐฐ ๋ฐฐ๋ฌ๊ณผ ์๊ฑฐํ๊ธฐ JS (0) | 2024.01.10 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ์ง๊ฟ JS (0) | 2024.01.09 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ JS (1) | 2024.01.07 |