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 |
Tags
- 프로젝트
- 라이브러리
- Server State
- React Query
- TypeScript
- React
- frontend
- shadcn
- 리액트프로젝트
- 실시간통신
- tanstack query
- @stomp/stompjs
- JavaScript
- 컴포넌트설계
- 상태 관리 라이브러리
- stompjs
- 코딩테스트
- 공식문서
- pnpm
- 수코딩
- 배열메서드
- 배열
- radixui
- 스나이퍼팩토리
- MDN
- npm
- sucoding
- 자바스크립트
- 프로젝트캠프
- 프론트엔드
Archives
- Today
- Total
yunicornlab
백준 1697번 숨바꼭질 JavaScript 풀이 [BFS] 본문
백준 1697번 숨바꼭질 문제를 자바스크립트로 BFS 알고리즘을 이용해서 풀어보았다.
https://www.acmicpc.net/problem/1697
// 큐 자료구조
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 input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let [n, k] = input[0].split(' ').map(Number);
// 큐
let queue = new Queue();
queue.enqueue(n);
// 방문처리
let distTable = new Array(100_001).fill(0);
while (queue.getLength() != 0) {
let position = queue.dequeue();
if (position == k) {
console.log(distTable[position]);
break;
}
for (let i of [position-1, position+1, position*2]) {
if (distTable[i] == 0) {
distTable[i] = distTable[position] + 1;
queue.enqueue(i);
}
}
}
'Coding Test > Practice' 카테고리의 다른 글
백준 21921번 블로그 JavaScript 풀이 [투포인터] (1) | 2024.07.22 |
---|---|
백준 11441번 합 구하기 JavaScript 풀이 [누적합] (0) | 2024.07.21 |
백준 2606번 바이러스 JavaScript 풀이 [DFS] (0) | 2024.07.21 |
백준 7490번 0 만들기 JavaScript 풀이 [백트래킹] (1) | 2024.07.21 |
백준 10974번 모든 순열 JavaScript 풀이 [백트래킹] (0) | 2024.07.20 |