티스토리 뷰

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

 

1926번: 그림

어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로

www.acmicpc.net

 

class Node {
    constructor(item) {
      this.item = item;
      this.next = null;
    }
  }
  
  class Queue {
    constructor() {
      this.head = null;
      this.tail = null;
      this.length = 0;
    }
  
    push(item) {
      const node = new Node(item);
      if (this.head == null) {
        this.head = node;
      } else {
        this.tail.next = node;
      }
  
      this.tail = node;
      this.length += 1;
    }
  
    pop() {
      const popItem = this.head;
      this.head = this.head.next;
      this.length -= 1;
      return popItem.item;
    }
  }
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n').map(v=>v.split(' ').map(Number));
const [N,M] = input.shift();

let art = input;
const dx = [0,0,1,-1];
const dy = [1,-1,0,0,];
let cnt = 0;
let max = 0;
for(let i = 0; i<N; i++){
    for(let j = 0; j<M; j++){
        if(art[i][j]==1){
            cnt++;
            const q = new Queue();
            q.push([i,j]);
            art[i][j] = 0;
            let size = 1;
            while(q.length>0){
                const [x,y] = q.pop();
                for(let k = 0; k<4; k++){
                    const nx = x + dx[k];
                    const ny = y + dy[k];
                    if(nx<0 ||nx>=N  || ny<0 || ny>=M || art[nx][ny]==0) continue;

                    art[nx][ny] = 0;
                    size++;
                    q.push([nx,ny])
                }

            }
            if(size>max) max = size;
        }
    }
}

console.log(cnt+'\n'+max)
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
글 보관함