๐ŸŽฒ ๋ฐฑ์ค€ 2468๋ฒˆ ์•ˆ์ „ ์˜์—ญ

Jeongeunยท2023๋…„ 6์›” 10์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
73/186

๋ฐฑ์ค€ 2468๋ฒˆ

๐Ÿ’Š ์•„๋ฌด ์ง€์—ญ๋„ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ๊ธฐ์ค€ ๋†’์ด๋ฅผ 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));

0๊ฐœ์˜ ๋Œ“๊ธ€