-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1012.js
81 lines (61 loc) · 2.37 KB
/
1012.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
function madeEmptyArray(x, y) {
let arr = new Array(x).fill(0);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(y).fill(0);
}
return arr
}
function main(input) {
const [T, ...testCases] = input;
const result = []
for (let i = 0; i < Number(T); i++) {
const [M, N, K] = testCases[0].split(" ").map(Number)
const ground = madeEmptyArray(M, N);
for (let j = 1; j <= K; j++) {
const [x, y] = testCases[j].split(" ").map(Number)
ground[x][y] = 1;
}
const visited = madeEmptyArray(M, N);
let count = 0;
const queue = [];
ground.forEach((arr, xIndex) => {
arr.forEach((val, yIndex) => {
if (!visited[xIndex][yIndex] && val === 1) {
queue.push([xIndex, yIndex]);
while (queue.length !== 0) {
const nowNode = queue.pop();
visited[nowNode[0]][nowNode[1]] = 1;
if (nowNode[1] + 1 < arr.length && !visited[nowNode[0]][nowNode[1] + 1] && ground[nowNode[0]][nowNode[1] + 1] === 1) {
queue.push([nowNode[0], nowNode[1] + 1])
}
if (nowNode[0] + 1 < ground.length && !visited[nowNode[0] + 1][nowNode[1]] && ground[nowNode[0] + 1][nowNode[1]] === 1) {
queue.push([nowNode[0] + 1, nowNode[1]])
}
if (nowNode[1] - 1 >= 0 && !visited[nowNode[0]][nowNode[1] - 1] && ground[nowNode[0]][nowNode[1] - 1] === 1) {
queue.push([nowNode[0], nowNode[1] - 1])
}
if (nowNode[0] - 1 >= 0 && !visited[nowNode[0] - 1][nowNode[1]] && ground[nowNode[0] - 1][nowNode[1]] === 1) {
queue.push([nowNode[0] - 1, nowNode[1]])
}
}
++count;
}
})
})
result.push(count)
testCases.splice(0, K + 1);
}
console.log(result.join('\n'))
}