๐ŸŽฒ ๋ฐฑ์ค€ 2583๋ฒˆ ์˜์—ญ ๊ตฌํ•˜๊ธฐ

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

๋ฐฑ์ค€

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

๋ฐฑ์ค€ 2583๋ฒˆ

์ฝ”๋“œ

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [M, N, K] = input.shift().split(" ").map(Number);

let arr = Array.from(new Array(M), () => new Array(N).fill(0));
for (let i = 0; i < K; i++) {
  const [lx, ly, rx, ry] = input[i].split(" ").map(Number);

  for (let y = 0; y < ry - ly; y++) {
    for (let x = 0; x < rx - lx; x++) {
      arr[ly + y][lx + x] = 1;
    }
  }
}

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

const DFS = (j, i) => {
  const willCheck = [[j, i]];
  let count = 1;

  while (willCheck.length) {
    const [pointX, pointY] = willCheck.pop();

    for (let i = 0; i < 4; i++) {
      const nextX = pointX + dir[i][0];
      const nextY = pointY + dir[i][1];

      if (
        nextX >= 0 &&
        nextX < N &&
        nextY >= 0 &&
        nextY < M &&
        arr[nextY][nextX] === 0
      ) {
        willCheck.push([nextX, nextY]);
        count++;
        arr[nextY][nextX] = -1;
    
      }
    }
  }
  return count;
};

const result = [];
for (let i = 0; i < M; i++) {
  for (let j = 0; j < N; j++) {
    if (arr[i][j] === 0) {
      arr[i][j] = -1;
      const count = DFS(j, i);
      result.push(count);
    }
  }
}
console.log(result.length);
console.log(result.sort((a, b) => a - b).join(" "));

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