๐ ์๋ฌด ์ง์ญ๋ ๋ฌผ์ ์ ๊ธฐ์ง ์์ ์๋ ์์ผ๋ฏ๋ก ๊ธฐ์ค ๋์ด๋ฅผ 0๋ถํฐ ํด์ผํ๋ค. ๊ธฐ์ค ๋์ด์ 100์ ํฌํจ์ํค์ง ์์๋ ๋๋ ์ด์ ๋ ๊ธฐ์ค ๋์ด๊ฐ 0์ผ๋ ์์ ์์ญ์ ๊ฐ์๊ฐ 1์ด ๋ณด์ฅ๋๊ธฐ๋๋ฌธ!
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const arr = input.map((item) => item.split(" ").map(Number));
const dir = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const DFS = (checked, y, x, line) => {
const willCheck = [[y, x]];
while (willCheck.length) {
const [y, x] = willCheck.pop();
checked[y][x] = 1;
for (let i = 0; i < 4; i++) {
const nextY = y + dir[i][0];
const nextX = x + dir[i][1];
if (
nextX >= 0 &&
nextX < N &&
nextY >= 0 &&
nextY < N &&
arr[nextY][nextX] > line &&
checked[nextY][nextX] === 0
) {
willCheck.push([nextY, nextX]);
}
}
}
};
const result = [];
for (let line = 0; line <= 99; line++) {
let checked = Array.from(new Array(N), () => new Array(N).fill(0));
let safe = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (checked[i][j] === 0 && arr[i][j] > line) {
DFS(checked, i, j, line);
safe++;
}
}
}
result.push(safe);
}
console.log(Math.max(...result));