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
- ๋ฐฑ์ค 2108 nodejs
- js ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- suspense ๋น๋๊ธฐ
- ์ค์ฝํ
- ๋ฐฑ์ค 1339๋ฒ ์๋ฐ์คํฌ๋ฆฝํธ
- next13 emotion
- js
- js ์ค์ฝํ
- suspense react-query
- app router emotion
- ๋ฐฑ์ค 1339๋ฒ js
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง ํ๊ณ
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- emotion RSC
- ์ฌ์ฉ์ฑ ๊ฐ์
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์์ด ์์ถ
- ํ๋ก๊ทธ๋๋จธ์ค ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- suspense ๋ณ๋ชฉํ์
- ๋ฐฑ์ค 2108 ์๋ฐ์คํฌ๋ฆฝํธ
- ๋ฐฑ์ค 1339๋ฒ nodejs
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด ์์ถ
- ๊ตฌ๋ฆํค
- ์ต์ ๋์ฒด์ด๋
- ์นด์นด์ค ์ฝํ
- ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ฝํ
- ๋ฆฌ์กํธ์ฟผ๋ฆฌ suspense
- js ๋ฌธ์์ด ์์ถ
- suspense ๋์์๋ฆฌ
- emtion app router
Archives
- Today
- Total
Lennon FE
[๋ฐฑ์ค 1260๋ฒ] DFS์ BFS - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) ๋ณธ๋ฌธ
๐ฅ Algorithm/Baekjoon
[๋ฐฑ์ค 1260๋ฒ] DFS์ BFS - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs)
Lennon 2022. 3. 18. 18:21728x90
๋ฐ์ํ
https://www.acmicpc.net/problem/1260
const { off } = require('process');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
class Graph {
constructor() {
this.node = {};
}
addVertex(vertex) {
if (!this.node[vertex]) {
this.node[vertex] = [];
}
}
addEdge(v1, v2) {
this.node[v1].push(v2);
this.node[v2].push(v1);
}
depthFirstSearch(start) {
const result = [];
const visited = {};
const node = this.node;
function dfs(vertex) {
if (!vertex) return;
result.push(vertex);
visited[vertex] = true;
node[vertex]
.sort((a, b) => a - b)
.forEach((v) => {
if (!visited[v]) {
visited[v] = true;
return dfs(v);
}
});
}
dfs(start);
return result;
}
breadthFirstSearch(start) {
const queue = [start];
const result = [];
const visited = {};
visited[start] = true;
while (queue.length) {
const value = queue.shift();
result.push(value);
this.node[value]
.sort((a, b) => a - b)
.forEach((v) => {
if (!visited[v]) {
visited[v] = true;
queue.push(v);
}
});
}
return result;
}
}
let input = [];
rl.on('line', function (line) {
input.push(line);
if (input.length === +input[0].split(' ')[1] + 1) {
rl.close();
}
}).on('close', function () {
const [num, ver, start] = input.shift().split(' ');
const vertex = input.map((v) => v.split(' ').map(Number));
const graph = new Graph();
for (let i = 1; i <= +num; i++) {
graph.addVertex(i);
}
vertex.sort((a, b) => a[0] - b[0]);
vertex.forEach((v) => {
graph.addEdge(v[0], v[1]);
});
console.log(graph.depthFirstSearch(start).join(' '));
console.log(graph.breadthFirstSearch(start).join(' '));
});
์ค์ํ ๊ฑด ์ ์๋ ๊ฐ์ ๋ค์ ์ ๋ ฌํ๊ณ , ๊ฐ์ ์ ๋ฃ์ ๊ฐ์ฒด ๋ด๋ถ์์๋ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋ ์ํ์ฌ์ผ ๋๋ค!
(์๋๋ฉด ์์ ๊ฐ ๋ง๊ณ , ์ง๋ฌธํ๊ธฐ์ ์๋ ๋ฐ๋ก๊ฐ ๋ง๋ ๊ณ์ ํ๋ฆฝ๋๋ค.)
์ฐ์ ๊ฐ์ ์ ์ซ์๋ณ๋ก ์ ๋ ฌํด์ ๊ฐ์ ๋ฃ์ด์ค๋ค.
Graph {
node: {
'1': [ 2, 3 ],
'2': [ 1, 5 ],
'3': [ 4, 1 ],
'4': [ 3, 5 ],
'5': [ 4, 2 ]
}
}
๊ทธ๋ฆฌ๊ณ bfs, dfs๋ฅผ ์ํํ ๋ ํด๋น ๊ฐ์ ๋ค์ ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก sortํด์ฃผ๋ฉด์ ํ๋ฉด ๋๋ค.
728x90
๋ฐ์ํ
'๐ฅ Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 11057๋ฒ] ์ค๋ฅด๋ง ์ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.03.18 |
---|---|
[๋ฐฑ์ค 1309๋ฒ] ๋๋ฌผ์ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.03.18 |
[๋ฐฑ์ค 17144๋ฒ] ๋ฏธ์ธ๋จผ์ง ์๋ ! - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.03.16 |
[๋ฐฑ์ค 1789๋ฒ] ์๋ค์ ํฉ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.03.14 |
[๋ฐฑ์ค 1932๋ฒ] ์ ์ ์ผ๊ฐํ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.03.11 |
Comments