Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Jiho/Day23/백준_10816.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require("fs");
const input = fs
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt")
.toString()
.trim()
.split("\n");

const N = parseInt(input[0]);
const carr = input[1].trim().split(" ").map(Number);
const M = parseInt(input[2]);
const nums = input[3].trim().split(" ").map(Number);

// Map 만들기
let map = new Map();
for (const val of carr) {
map.set(val, (map.get(val) | 0) + 1);
}

// 결과 만들기
const result = [];
for (const num of nums) {
result.push(map.get(num) | 0);
}
console.log(result.join(" "));
63 changes: 63 additions & 0 deletions Jiho/Day23/백준_4963.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fs = require("fs");
const input = fs
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt")
.toString()
.trim()
.split("\n");

// 저장
let answers = [];
while (true) {
const [w, h] = input.shift()?.split(" ").map(Number);

if (w === 0 && h === 0) {
break;
}
let map = Array.from(new Array(h), () => new Array(w));
let visited = Array.from(new Array(h), () => new Array(w).fill(false));

for (let i = 0; i < h; i++) {
const arr = input.shift()?.split(" ").map(Number);
for (let j = 0; j < w; j++) {
map[i][j] = arr[j];
}
}

const BFS_FindConnected = (startRow, startCol, map, visited) => {
const dr = [0, 1, 1, 1, 0, -1, -1, -1];
const dc = [1, 1, 0, -1, -1, -1, 0, 1];
visited[startRow][startCol] = true;
let queue = [[startRow, startCol]];
while (queue.length) {
const [row, col] = queue.shift();

for (let i = 0; i < 8; i++) {
const [nrow, ncol] = [row + dr[i], col + dc[i]];
if (
nrow >= 0 &&
nrow < h &&
ncol >= 0 &&
ncol < w &&
!visited[nrow][ncol] &&
map[nrow][ncol] === 1
) {
visited[nrow][ncol] = true;
queue.push([nrow, ncol]);
}
}
}
};

// Execute BFS
let answer = 0;
for (let row = 0; row < h; row++) {
for (let col = 0; col < w; col++) {
if (!visited[row][col] && map[row][col] === 1) {
answer++;
BFS_FindConnected(row, col, map, visited);
}
}
}
answers.push(answer);
}
console.log(answers.join("\n"));
Loading