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
- frontend
- React Query
- 유데미
- sucoding
- tanstack query
- 프론트엔드
- 프론트엔드 개발
- 스나이퍼팩토리
- 수코딩
- 웅진씽크빅
- 리액트프로젝트
- STATE
- 공식문서
- 상태 관리 라이브러리
- 프로젝트캠프
- TypeScript
- React
- 개발
- Server State
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 |