티스토리 뷰

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

 

2630번: 색종이 만들기

첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.

www.acmicpc.net

큐로 풀었음

const fs = require('fs');
const [n,...arr] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");

const N = +n
const board = arr.map(v=>v.split(' ').map(v=>+v))


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;
      this.head.next = this.tail;
    }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;
  }

  size(){
    return this.length;
  }

  empty(){
    if(this.length==0){
      return 1;
    }else{
      return 0;
    }
  }

  front(){
    if(this.empty()==1) return -1;
    return this.head.item; 
  }

  back(){
    if(this.empty()==1) return -1;
    return this.tail.item; 
  }
}


let q=new Queue();
q.push([0,0,N]);
let answer= [0,0];
while(!q.empty()){
  let [y,x,size] = q.pop();
  let first = board[y][x];
  let same = true;

  for(let i = y; i<y+size; i++){
    for(let j = x; j<x+size; j++){
      if(board[i][j]!=first) {
        same =false;
        break;
      }
    }
    if(!same)break;
  }
  if(same){
    answer[first]++;
  }else{
    q.push([y,x,size/2])
    q.push([y+size/2,x,size/2])
    q.push([y,x+size/2,size/2])
    q.push([y+size/2,x+size/2,size/2])
  }
}

console.log(answer.join('\n'))
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
29 30
글 보관함