Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- React
- shadcn
- 프론트엔드
- 배열메서드
- React Query
- 리액트프로젝트
- radixui
- Server State
- 프로젝트
- 자바스크립트
- 공식문서
- 코딩테스트
- tanstack query
- TypeScript
- 상태 관리 라이브러리
- 프로젝트캠프
- sucoding
- JavaScript
- stompjs
- MDN
- 스나이퍼팩토리
- npm
- 배열
- @stomp/stompjs
- 컴포넌트설계
- 실시간통신
- 라이브러리
- pnpm
- 수코딩
- frontend
Archives
- Today
- Total
yunicornlab
백준 16953번 A -> B JavaScript 풀이 [BFS] 본문
백준 16953번 A -> B 문제를 자바스크립트로 BFS 알고리즘을 이용해서 풀어보았다.
https://www.acmicpc.net/problem/16953
let fs = require("fs");
let input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");
class Queue {
constructor() {
this.items = {};
this.head = 0;
this.tail = 0;
}
enqueue(element) {
this.items[this.tail] = element;
this.tail++;
}
dequeue() {
const element = this.items[this.head];
delete this.items[this.head];
this.head++;
return element;
}
getLength() {
return this.tail - this.head;
}
}
let [a, b] = input[0].split(' ').map(Number);
let visited = new Set([a]);
queue = new Queue();
queue.enqueue([a, 1]);
let result = -1;
while (queue.getLength() != 0) {
let [x, count] = queue.dequeue();
let next;
for (let i=0; i<2; i++) {
if (i == 0) next = x * 2;
else next = Number(x.toString() + "1");
if (!visited.has(next) && next <= b) {
visited.add(next)
queue.enqueue([next, count + 1])
if (next == b) {
result = count;
break;
}
}
}
}
console.log(result == -1 ? -1 : result + 1)
'Coding Test > Practice' 카테고리의 다른 글
백준 3190번 뱀 JavaScript 풀이 [BFS] (0) | 2024.07.27 |
---|---|
백준 16234번 인구 이동 JavaScript 풀이 [BFS] (0) | 2024.07.27 |
백준 2638번 치즈 JavaScript 풀이 [BFS] (0) | 2024.07.27 |
백준 5567번 결혼식 JavaScript 풀이 [BFS] (0) | 2024.07.27 |
백준 5214번 환승 JavaScript 풀이 [BFS] (0) | 2024.07.27 |