티스토리 뷰

https://www.acmicpc.net/problem/2583

 

2583번: 영역 구하기

첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split('\n').map(v=>v.split(' ').map(Number));
const [N,M,K] = input.shift();
const dir = [[1,0],[-1,0],[0,1],[0,-1]]
//모눈종이 만들기
let board = Array.from(Array(M),()=>Array(N).fill(0))
for(let i = 0; i<K; i++){
  const [startY,startX,endY,endX] = input[i];
  for(let y = startY; y<endY; y++){
    for(let x = startX; x<endX; x++){
      board[y][x] = 1;
    }
  }
}

// 영역 나누기
const area =[];
for(let i = 0; i<M; i++){
  for(let j = 0; j<N; j++){
    if(board[i][j]==0){
      let q = [];
      board[i][j]=1;
      q.push([i,j])
      let cnt = 1;
      while(q.length>0){
        const [a,b]=q.pop();
        dir.forEach(v=>{
          const [c,d] = v;
          if((a+c)<M && (a+c)>-1 && (b+d)<N && (b+d)>-1 && board[a+c][b+d]==0){
            board[a+c][b+d]=1;
            q.push([a+c,b+d])
            cnt++;
          }
        })
      }
      area.push(cnt)
    }
  }
}

let answer = `${area.length}\n`+area.sort((a,b)=>a-b).join(' ')

console.log(answer)
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함