백준 2583 영역구하기 JavaScript (Node.js)

1

Problem Solving

목록 보기
40/49
post-thumbnail

문제

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

풀이

  1. 백준 단지번호붙이기? 랑 비슷한것 같다.
  2. 주어진 좌표 범위를 1로 채워준다.
  3. 처음부터 끝까지 for문으로 돌면서 BFS를 수행한다.
  4. BFS를 수행할때 반복문의 횟수가 영역의 갯수다.
  5. 영역의 갯수를 따로 배열로 저장해놓고, 배열의 길이와 배열의 요소들을 출력해준다.
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const [N, M, K] = input.shift().trim().split(" ").map(Number);
const area = input.map((e) => e.trim().split(" ").map(Number));
const graph = Array.from(Array(N), () => Array(M).fill(0));
const dx = [-1, 1, 0, 0];
const dy = [0, 0, -1, 1];

/* 그래프 그리기 */
for (const [sx, sy, ex, ey] of area) {
  for (let i = N - ey; i <= N - sy - 1; i++) {
    for (let j = sx; j <= ex - 1; j++) {
      graph[i][j] = 1;
    }
  }
}

/* BFS */
let q = new Array();
const BFS = (y, x) => {
  q.push([y, x]);
  let ret = 0;
  graph[y][x] = 1;
  while (q.length) {
    const [y, x] = q.shift();
    for (let i = 0; i < 4; i++) {
      const nx = dx[i] + x;
      const ny = dy[i] + y;
      if (0 <= nx && nx < M && 0 <= ny && ny < N && !graph[ny][nx]) {
        graph[ny][nx] = 1;
        q.push([ny, nx]);
      }
    }
    ret++;
  }
  return ret;
};

let answer = [];
for (let i = 0; i < graph.length; i++) {
  for (let j = 0; j < graph[0].length; j++) {
    if (!graph[i][j]) {
      answer.push(BFS(i, j));
    }
  }
}
console.log(answer.length);
console.log(answer.sort((a, b) => a - b).join(" "));

0개의 댓글