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
- 배열메서드
- 자바스크립트
- radixui
- Server State
- stompjs
- pnpm
- 리액트프로젝트
- 컴포넌트설계
- 공식문서
- 코딩테스트
- tanstack query
- 라이브러리
- 배열
- React Query
- 프론트엔드
- 스나이퍼팩토리
- npm
- @stomp/stompjs
- 수코딩
- TypeScript
- JavaScript
- sucoding
- frontend
- shadcn
- 실시간통신
- 프로젝트캠프
- React
- 상태 관리 라이브러리
- MDN
- 프로젝트
Archives
- Today
- Total
yunicornlab
백준 5567번 결혼식 JavaScript 풀이 [BFS] 본문
백준 5567번 결혼식 문제를 자바스크립트로 BFS 알고리즘을 이용해서 풀어보았다.
https://www.acmicpc.net/problem/5567
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 n = Number(input[0]);
let m = Number(input[1]);
let graph = new Array(n+1).fill().map(_ => []);
for (t=2; t<=m+1; t++) {
let [f1, f2] = input[t].split(' ').map(Number);
graph[f1].push(f2)
graph[f2].push(f1)
}
queue = new Queue();
queue.enqueue(1);
let visited = new Array(n+1).fill(0);
while (queue.getLength() != 0) {
let cur = queue.dequeue();
for (next of graph[cur]) {
if (visited[next] == 0) {
visited[next] = visited[cur] + 1;
queue.enqueue(next)
}
}
}
// 자기자신은 0으로 바꿔놓기
visited[1] = 0;
console.log(visited.filter(v => 1 <= v && v<=2).length)
'Coding Test > Practice' 카테고리의 다른 글
백준 16953번 A -> B JavaScript 풀이 [BFS] (0) | 2024.07.27 |
---|---|
백준 2638번 치즈 JavaScript 풀이 [BFS] (0) | 2024.07.27 |
백준 5214번 환승 JavaScript 풀이 [BFS] (0) | 2024.07.27 |
백준 18352번 특정 거리의 도시 찾기 JavaScript 풀이 [BFS] (0) | 2024.07.26 |
백준 18405번 경쟁적 전염 JavaScript 풀이 [BFS] (0) | 2024.07.26 |