๋ฐฑ์ค 1012๋ฒ
์ฝ๋
๐งธ ์ด์ ํ์๋ ๋ฌธ์ ๋ ๊ฑฐ์ ๋๊ฐ์์ ํผ์์ ํ ์ ์์๋ค! ๊ทธ๋๋ ๋ฌธ์ ํธ๋ ์๊ฐ์ ์ค์ฌ์ผํ ๊ฒ ๊ฐ๋ค.
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const dir = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const num = +input.shift();
for (let i = 0; i < num; i++) {
let block = 0;
let willChecked = [];
const [X, Y, K] = input.shift().split(" ").map(Number);
let checked = Array.from(new Array(Y), () => new Array(X).fill(0));
let arr = Array.from(new Array(Y), () => new Array(X).fill(0));
for (let k = 0; k < K; k++) {
const [x, y] = input.shift().split(" ").map(Number);
arr[y][x] = 1;
}
for (let y = 0; y < Y; y++) {
for (let x = 0; x < X; x++) {
if (arr[y][x] === 1 && checked[y][x] !== 1) {
//--- BFS
willChecked.push([y, x]);
while (willChecked.length) {
const [pointY, pointX] = willChecked.shift();
if (checked[pointY][pointX] === 0) {
checked[pointY][pointX] = 1;
for (let i = 0; i < 4; i++) {
nextY = pointY + dir[i][0];
nextX = pointX + dir[i][1];
if (
nextX >= 0 &&
nextY >= 0 &&
nextX < X &&
nextY < Y &&
arr[nextY][nextX] === 1
) {
willChecked.push([nextY, nextX]);
}
}
}
}
// BFS ---
block++;
}
}
}
console.log(block);
}