[백준] 2630. 색종이 만들기

상현·2023년 11월 13일
3

코딩테스트

목록 보기
16/30
post-thumbnail

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

var fs = require('fs');
var input = fs.readFileSync('/dev/stdin').toString().split('\n');

let N = +input.shift();

input = input.map(r => r.split(" ").map(Number));

let whiteCount = 0;
let blueCount = 0;

const checkAllSame = (x, y, n) => {
  let sum = 0;

  for (let i = x; i < n + x; i++) {
    for (let j = y; j < n + y; j++) {
      let value = +input[i][j];
      sum += value
    }
  }
  
  // 합이 0 => 흰색 종이
  // 합이 종이 넓이 => 파란색 종이
  let isWhite = sum === 0;
  let isBlue = sum === n * n;

  if (isWhite) whiteCount++;
  if (isBlue) blueCount++;
  
  // 하얀색도 아니고 파란색도 아니면 해당 종이에서 4등분해서 재귀
  if (!isWhite && !isBlue) {
    n /= 2;
    if (n < 1) return;
    checkAllSame(x, y, n);
    checkAllSame(x, y + n, n);
    checkAllSame(x + n, y, n);
    checkAllSame(x + n, y + n, n);
  }

}

checkAllSame(0, 0, N)

console.log(whiteCount)
console.log(blueCount)

재귀 문제는 언제 풀어도 어렵다....ㅠㅠ 머릿속에 잘 그려지지 않는다.

profile
프론트엔드 개발자 🧑🏻‍💻 https://until.blog/@love

0개의 댓글