-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2234.js
More file actions
84 lines (71 loc) · 1.89 KB
/
b2234.js
File metadata and controls
84 lines (71 loc) · 1.89 KB
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
82
83
84
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const [N, M] = input[0].split(" ").map(Number);
const board = input.slice(1).map((row) => row.split(" ").map(Number));
let visited = Array(M)
.fill(0)
.map(() => Array(N).fill(false));
// 1111 남동북서
function bfs(x, y, visited) {
const dx = [0, -1, 0, 1];
const dy = [-1, 0, 1, 0];
let size = 0;
const localVisited =
visited ||
Array(M)
.fill(0)
.map(() => Array(N).fill(false));
const q = [];
q.push([x, y]);
localVisited[x][y] = true;
while (q.length) {
const [cx, cy] = q.shift();
const wall = board[cx][cy].toString(2).padStart(4, "0").split("");
size += 1;
for (let i = 0; i < 4; i++) {
if (wall[wall.length - 1 - i] === "0") {
// console.log(cx, cy, board[cx][cy], wall, i);
const nx = dx[i] + cx;
const ny = dy[i] + cy;
// checkRange
// localVisited
if (!(0 <= nx && nx < M && 0 <= ny && ny < N)) continue;
if (localVisited[nx][ny]) continue;
localVisited[nx][ny] = true;
q.push([nx, ny]);
}
}
}
return size;
}
let rmCnt = 0;
let maxSize = 0;
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
if (!visited[i][j]) {
maxSize = Math.max(maxSize, bfs(i, j, visited));
rmCnt += 1;
}
}
}
let combinedSize = 0;
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
for (let k = 0; k < 4; k++) {
if (board[i][j] - 2 ** k >= 0) {
const wall = (board[i][j] - 2 ** k)
.toString(2)
.padStart(4, "0")
.split("");
board[i][j] -= 2 ** k;
if (wall[wall.length - 1 - k] === "0") {
combinedSize = Math.max(combinedSize, bfs(i, j));
}
board[i][j] += 2 ** k;
}
}
}
}
console.log(rmCnt);
console.log(maxSize);
console.log(combinedSize);