본문 바로가기
알고리즘 & 자료구조/백준

백준 2630

by 신재권 2022. 4. 10.
package baekjoon;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main2630 {

	public static int N;
	public static int[][] map;
	public static int white = 0;
	public static int blue = 0;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		N = Integer.parseInt(br.readLine());
		map = new int[N][N];
		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(br.readLine());
			for (int j = 0; j < N; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		confetti(0,0,N);
		System.out.println(white);
		System.out.println(blue);
	}

	public static void confetti(int y, int x, int size) {
		if (isCheck2630(y, x, size)) {
			if(map[y][x] == 0){
				white++;
			} else if (map[y][x] == 1) {
				blue++;
			}
			return;
		}

		int nSize = size/2;

		confetti(y, x, nSize);
		confetti(y, x+nSize, nSize);
		confetti(y+nSize, x, nSize);
		confetti(y+nSize, x+nSize, nSize);

		return;
	}

	public static boolean isCheck2630(int y, int x, int size) {
		int val = map[y][x];
		for (int i = y; i < y + size; i++) {
			for (int j = x; j < x + size; j++) {
				if (val != map[i][j]) {
					return false;
				}
			}
		}
		return true;
	}

}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 1780  (0) 2022.04.12
백준 17829  (0) 2022.04.11
백준 2798_1  (0) 2022.03.22
백준 1003  (0) 2022.03.21
백준 14889  (0) 2022.03.13