Skip to content

Commit 747ce18

Browse files
committed
BOJ1303- 전쟁 - 전투
source:99746926f8a89cca1f05f1d9a5b7b880baa2193a
1 parent 2be583a commit 747ce18

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const fs = require("fs");
2+
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
3+
const [size, ...input] = fs
4+
.readFileSync(filePath)
5+
.toString()
6+
.trim()
7+
.split("\n");
8+
9+
const [col, row] = size.split(" ").map(Number);
10+
11+
const map = input.map(line => line.split(""));
12+
const visited = Array.from({ length: row }, () => Array(col).fill(false));
13+
14+
const dx = [-1, 0, 1, 0];
15+
const dy = [0, -1, 0, 1];
16+
const stack = [];
17+
let color;
18+
let white = 0;
19+
let black = 0;
20+
21+
for (let i = 0; i < row; i++) {
22+
for (let j = 0; j < col; j++) {
23+
if (visited[i][j]) continue;
24+
25+
let cnt = 1;
26+
color = map[i][j];
27+
stack.push([i, j])
28+
visited[i][j] = true;
29+
30+
while (stack.length > 0) {
31+
const [x, y] = stack.pop();
32+
33+
for (let t = 0; t < 4; t++) {
34+
let cx = x + dx[t];
35+
let cy = y + dy[t];
36+
37+
if (cx<0 || cy<0 ||cx >= row || cy >= col || visited[cx][cy] || map[cx][cy] != color) continue;
38+
39+
stack.push([cx, cy])
40+
visited[cx][cy] = true;
41+
cnt++;
42+
}
43+
}
44+
if (color === 'W') {
45+
white += cnt * cnt;
46+
} else black += cnt * cnt;
47+
}
48+
49+
}
50+
51+
console.log(white+" "+ black)

0 commit comments

Comments
 (0)