๐ŸŽฒ ๋ฐฑ์ค€ 1926๋ฒˆ ๊ทธ๋ฆผ

Jeongeunยท2023๋…„ 7์›” 13์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
92/187

๋ฐฑ์ค€ 1926๋ฒˆ

๐Ÿงธ ๊ทธ๋™์•ˆ ๋งŽ์ด ํ’€์–ด๋ณธ ์œ ํ˜•์ด๋ผ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค!

์ฝ”๋“œ

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [Y, X] = input.shift().split(" ").map(Number);
const arr = input.map((item) => item.split(" ").map(Number));

const result = [];
const dir = [
  [0, 1],
  [0, -1],
  [1, 0],
  [-1, 0],
];

const DFS = (i, j) => {
  willCheck = [[i, j]];
  arr[i][j] = -1;
  let count = 0;
  while (willCheck.length) {
    const [pointY, pointX] = willCheck.pop();
    count++;
    for (let d = 0; d < 4; d++) {
      const nextY = pointY + dir[d][0];
      const nextX = pointX + dir[d][1];
      if (
        nextX >= 0 &&
        nextX < X &&
        nextY >= 0 &&
        nextY < Y &&
        arr[nextY][nextX] === 1
      ) {
        willCheck.push([nextY, nextX]);
        arr[nextY][nextX] = -1;
      }
    }
  }
  result.push(count);
};

for (let i = 0; i < Y; i++) {
  for (let j = 0; j < X; j++) {
    if (arr[i][j] === 1) {
      DFS(i, j);
    }
  }
}

console.log(result.length);
console.log(result.length ? Math.max(...result) : 0);

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