Skip to content

Commit 6d771e6

Browse files
committed
2 parents 77624ed + 8f00be1 commit 6d771e6

8 files changed

+338
-0
lines changed

Diff for: 3주차/박종운/실버1__미로_탐색.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const local_input = `
2+
4 6
3+
101111
4+
101010
5+
101011
6+
111011
7+
`;
8+
9+
const input = process.execArgv.includes("--stack-size=65536")
10+
? require("fs").readFileSync("dev/stdin").toString()
11+
: local_input;
12+
13+
const lines = input.trim().split('\n');
14+
15+
const [N, M] = lines[0].split(' ').map(Number);
16+
const map = lines.slice(1).map(el => el.split(''));
17+
const history = new Set(['0,0']);
18+
const tree = [
19+
new Set(['0,0']),
20+
]
21+
22+
for(let i=1; i<=N*M; i++) {
23+
const lastSet = tree[tree.length - 1];
24+
const newSet = new Set();
25+
lastSet.forEach(str => {
26+
const [y, x] = str.split(',').map(Number);
27+
28+
if(y-1 >= 0 && y-1 < N && map[y-1][x] === '1') { // 위
29+
const str = `${y-1},${x}`;
30+
31+
if(!history.has(str)) {
32+
newSet.add(str)
33+
history.add(str)
34+
}
35+
}
36+
if(y+1 >= 0 && y+1 < N && map[y+1][x] === '1') { // 아래
37+
const str = `${y+1},${x}`;
38+
39+
if(!history.has(str)) {
40+
newSet.add(str)
41+
history.add(str)
42+
}
43+
}
44+
if(x-1 >= 0 && x-1 < M && map[y][x-1] === '1') { // 왼쪽
45+
const str = `${y},${x-1}`;
46+
47+
if(!history.has(str)) {
48+
newSet.add(str)
49+
history.add(str)
50+
}
51+
}
52+
if(x+1 >= 0 && x+1 < M && map[y][x+1] === '1') { // 오른쪽
53+
const str = `${y},${x+1}`;
54+
55+
if(!history.has(str)) {
56+
newSet.add(str)
57+
history.add(str)
58+
}
59+
}
60+
61+
})
62+
63+
if(newSet.has(`${N-1},${M-1}`)) {
64+
console.log(tree.length + 1)
65+
return null
66+
}
67+
68+
tree.push(newSet);
69+
}

Diff for: 3주차/이진아/레벨3__가장먼노드.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution (n, edge) {
2+
//1. 연결리스트 생성
3+
const connects = new Array(n).fill().map(_ => []);
4+
5+
//2. 양방향으로 연결리스트를 채워줌
6+
//edge 배열의 각 요소에 대해 반복
7+
for(const e of edge) {
8+
connects[e[0]-1].push(e[1]-1);
9+
connects[e[1]-1].push(e[0]-1);
10+
}
11+
12+
const visited = [1];
13+
const queue = [0];
14+
while(queue.length) {
15+
const cur = queue.shift();
16+
17+
for(const next of connects[cur]) {
18+
if(!visited[next]) {
19+
visited[next] = visited[cur] + 1;
20+
queue.push(next);
21+
}
22+
}
23+
}
24+
//visited 배열에서 최대 거리를 구한다
25+
const max = Math.max(...visited);
26+
//최대 거리와 같은 값을 가지는 요소들의 개수를 세어 반환
27+
return visited.filter(el => el === max).length;
28+
}

Diff for: 3주차/이진아/실버1__미로탐색.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let input = require('fs').readFileSync('dev/stdin').toString().trim().split('\n');
2+
3+
const [n, m] = input.shift().split(" ");
4+
let graph = input.map(arr => arr.split("").map(x => +x));
5+
6+
const BFS = (n, m, arr) => {
7+
const dx = [-1, 0, 1, 0];
8+
const dy = [0, 1, 0, -1];
9+
10+
let queue = [];
11+
queue.push({x: 0, y: 0});
12+
13+
while (queue.length) {
14+
const target = queue.shift();
15+
for (let i = 0; i < 4; i++) {
16+
const nextX = target.x + dx[i];
17+
const nextY = target.y + dy[i];
18+
19+
if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= m) {
20+
continue;
21+
}
22+
23+
if (arr[nextX][nextY] !== 1) {
24+
continue;
25+
}
26+
27+
arr[nextX][nextY] = arr[target.x][target.y] + 1;
28+
queue.push({x: nextX, y: nextY});
29+
}
30+
}
31+
return arr[n-1][m-1];
32+
}
33+
34+
const answer = BFS(n, m, graph)
35+
console.log(answer)

Diff for: 4주차/박종운/골드4__사회망_서비스.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const readline = require('readline');
2+
3+
const rl = readline.createInterface({
4+
input: process.execArgv.includes("--stack-size=65536") ?
5+
process.stdin : require('fs').createReadStream(__dirname + '/input.txt'),
6+
output: process.stdout
7+
});
8+
9+
let N;
10+
let graph;
11+
let dp;
12+
let visited;
13+
14+
function dfs(cur) {
15+
visited[cur] = true;
16+
17+
for (const child of graph[cur]) {
18+
if (!visited[child]) {
19+
dfs(child);
20+
dp[cur][0] += dp[child][1];
21+
dp[cur][1] += Math.min(...dp[child]);
22+
}
23+
}
24+
}
25+
26+
rl.question('', input => {
27+
N = parseInt(input);
28+
graph = Array.from({ length: N + 1 }, () => []);
29+
dp = Array.from({ length: N + 1 }, () => [0, 1]);
30+
visited = Array(N + 1).fill(false);
31+
32+
rl.on('line', line => {
33+
const [u, v] = line.split(' ').map(Number);
34+
graph[u].push(v);
35+
graph[v].push(u);
36+
}).on('close', () => {
37+
dfs(1);
38+
console.log(Math.min(...dp[1]));
39+
40+
process.exit();
41+
});
42+
});

Diff for: 4주차/박종운/레벨3__길_찾기_게임.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution(nodeinfo) {
2+
const nodes = nodeinfo.map((el, i) => [i+1, ...el]);
3+
4+
const sorted = nodes.sort((a, b) => b[2] === a[2] ? a[1] - b[1] : b[2] - a[2]);
5+
6+
const preorder = [];
7+
const postorder = [];
8+
9+
function traverse(arr) {
10+
if(!arr.length) return;
11+
12+
const current = arr[0];
13+
14+
preorder.push(current[0]);
15+
16+
const newLeft = arr.filter(el => el[2] < current[2] && el[1] < current[1]);
17+
const newRight = arr.filter(el => el[2] < current[2] && el[1] > current[1] );
18+
19+
traverse(newLeft);
20+
traverse(newRight);
21+
22+
postorder.push(current[0]);
23+
}
24+
25+
traverse(sorted);
26+
27+
return [preorder, postorder];
28+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const local_input = `
2+
7
3+
1 6
4+
6 3
5+
3 5
6+
4 1
7+
2 4
8+
4 7
9+
`;
10+
11+
const input = process.execArgv.includes("--stack-size=65536")
12+
? require("fs").readFileSync("dev/stdin").toString()
13+
: local_input;
14+
15+
const lines = input.trim().split('\n');
16+
17+
const N = Number(lines[0]);
18+
const edges = lines.slice(1).map(el => el.split(' ').map(Number));
19+
20+
const graph = {};
21+
22+
edges.forEach(el => {
23+
if(!graph[el[0]]) graph[el[0]] = [];
24+
if(!graph[el[1]]) graph[el[1]] = [];
25+
graph[el[0]].push(el[1])
26+
graph[el[1]].push(el[0])
27+
})
28+
29+
const queue = [1];
30+
const visited = new Set([1]);
31+
const parentNodes = [];
32+
33+
while(queue.length) {
34+
const currentNode = queue.shift();
35+
const childNodes = graph[currentNode].filter(childNode => !visited.has(childNode));
36+
37+
childNodes.forEach(childNode => {
38+
parentNodes[childNode] = currentNode;
39+
queue.push(childNode);
40+
})
41+
42+
visited.add(currentNode);
43+
}
44+
45+
console.log(parentNodes.slice(2).join('\n'))
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const local_input = `
2+
8
3+
1 2
4+
1 3
5+
1 4
6+
2 5
7+
2 6
8+
4 7
9+
4 8
10+
`;
11+
12+
const input = process.execArgv.includes("--stack-size=65536")
13+
? require("fs").readFileSync("dev/stdin").toString()
14+
: local_input;
15+
const inputs = input
16+
.trim()
17+
.split("\n")
18+
.slice(1)
19+
.map((v) => v.split(" ").map((v2) => Number(v2)));
20+
21+
const n = Number(input.trim().split("\n")[0]);
22+
const dp = Array.from({ length: n + 1 }, () =>
23+
Array.from({ length: 2 }, () => 0)
24+
);
25+
26+
const map = {};
27+
const visited = Array.from({ length: n + 1 }, () => false);
28+
inputs.forEach((v) => {
29+
if (map[v[0]]) map[v[0]].push(v[1]);
30+
else map[v[0]] = [v[1]];
31+
if (map[v[1]]) map[v[1]].push(v[0]);
32+
else map[v[1]] = [v[0]];
33+
});
34+
35+
const dfs = (cur) => {
36+
visited[cur] = true;
37+
dp[cur][0] = 0;
38+
dp[cur][1] = 1;
39+
40+
for (const node of map[cur]) {
41+
if (visited[node]) continue;
42+
dfs(node);
43+
dp[cur][0] += dp[node][1];
44+
dp[cur][1] += Math.min(dp[node][0], dp[node][1]);
45+
}
46+
};
47+
48+
dfs(1);
49+
console.log(Math.min(dp[1][0], dp[1][1]));
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const local_input = `
2+
7
3+
1 6
4+
6 3
5+
3 5
6+
4 1
7+
2 4
8+
4 7
9+
`;
10+
11+
const input = process.execArgv.includes("--stack-size=65536")
12+
? require("fs").readFileSync("dev/stdin").toString()
13+
: local_input;
14+
15+
const inputs = input.trim().split("\n");
16+
let cnt = 0;
17+
const n = Number(inputs[0]);
18+
const arr = inputs.slice(1).map((v) => v.split(" ").map((v2) => Number(v2)));
19+
const map = {};
20+
arr.forEach((v) => {
21+
if (map[v[0]]) map[v[0]].push(v[1]);
22+
else map[v[0]] = [v[1]];
23+
if (map[v[1]]) map[v[1]].push(v[0]);
24+
else map[v[1]] = [v[0]];
25+
});
26+
const parents = Array.from({ length: n + 1 }, () => 0);
27+
const visited = Array.from({ length: n + 1 }, () => false);
28+
29+
const que = [[1, map[1]]];
30+
31+
while (que.length > 0) {
32+
const [cur, nodeArr] = que.shift();
33+
34+
visited[cur] = true;
35+
36+
for (const node of nodeArr) {
37+
if (visited[node]) continue;
38+
parents[node] = cur;
39+
que.push([node, map[node]]);
40+
}
41+
}
42+
console.log(parents.slice(2).reduce((a, v) => a + v + "\n", ""));

0 commit comments

Comments
 (0)