백준 1780

旅人·2023년 3월 27일
0

문제


Code

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class P1780 {

	static int[][] board;
    
    /*
    counts[0] : -1 종이
    counts[1]: 0 종이
    counts[2]: 1 종이
    */
	static int[] counts = new int[3];
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		StringTokenizer st;

		int N = Integer.parseInt(br.readLine());
		board = new int[N][N];

		for(int i = 0; i < N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for(int j = 0; j < N; j++) {
				board[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		partition(0, 0, N);
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < counts.length; i++) {
			sb.append(counts[i]).append('\n');
		}
		
		bw.write(sb.toString());
		
		br.close();
		bw.flush();
		bw.close();
	}

	// 해당 구역이 모두 같은 숫자인지 확인
	private static boolean sameNumber(int row, int col, int size) {
		int value = board[row][col];

		for(int i = row; i < row + size; i++) {
			for(int j = col; j < col + size; j++) {
				if(board[i][j] != value) {
					return false;
				}
			}
		}
		return true;
	}

	private static void partition(int row, int col, int size) {
    	// 1) 해당 구역이 모두 같은 숫자 
		if(sameNumber(row, col, size)) {
			if(board[row][col] == -1) {
				counts[0]++;
			} else if(board[row][col] == 0) {
				counts[1]++;
			} else {
				counts[2]++;
			}
			return;
		}
        
        // 2) 해당 구역에 다른 숫자 존재
		
		int nextSize = size / 3;
		
		partition(row, col, nextSize);
		partition(row, col + nextSize, nextSize);
		partition(row, col + nextSize * 2, nextSize);
		partition(row + nextSize, col, nextSize);
		partition(row + nextSize, col + nextSize, nextSize);
		partition(row + nextSize, col + nextSize * 2, nextSize);
		partition(row + nextSize * 2, col, nextSize);
		partition(row + nextSize * 2, col + nextSize, nextSize);
		partition(row + nextSize * 2, col + nextSize * 2, nextSize);
		
	}

}
profile
一期一会

0개의 댓글