문제보기
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
let input = fs
.readFileSync(filePath)
.toString()
.trim()
.split("\n")
.map((item) => item.split(" ").map((value) => +value));
const N = input.shift()[0];
const map = input.slice();
const visited = Array.from(Array(N + 1), () => Array(N).fill(false));
let direction = [
[-1, 0],
[0, 1],
[1, 0],
[0, -1],
];
let result = [];
let max = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
let info = map[i][j];
if (max < info) {
max = info;
}
}
}
const DFS = (map, visited, safeList) => {
let stack = [safeList];
let count = 0;
while (stack.length) {
let [x, y] = stack.pop();
if (!visited[x][y]) {
visited[x][y] = true;
count++;
}
for (let i = 0; i < direction.length; i++) {
let [dx, dy] = [x + direction[i][0], y + direction[i][1]];
if (dx < 0 || dy < 0 || dx >= N || dy >= N) continue;
if (!visited[dx][dy] && map[dx][dy] > 0) {
count++;
visited[dx][dy] = true;
stack.push([dx, dy]);
}
}
}
return [count, map, visited];
};
for (let water = 0; water <= max; water++) {
let copyMap = map.map((item) => item.slice());
let copyVisited = visited.map((item) => item.slice());
let safeList = [];
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (copyMap[i][j] <= water) {
copyMap[i][j] = 0;
copyVisited[i][j] = true;
} else {
safeList.push([i, j]);
}
}
}
let count = 0;
for (let i = 0; i < safeList.length; i++) {
let [value, newMap, newVisited] = DFS(
copyMap,
copyVisited,
safeList[i]
);
if (value) {
count++;
}
copyMap = newMap;
copyVisited = newVisited;
}
result.push(count);
}
console.log(Math.max(...result));