알고리즘

[백준, 자바, 1780번] 종이의 개수

hminor 2024. 10. 23. 15:33
반응형

풀이

  • 이번 문제는 크게 설명할 부분은 없지만,
  • 그냥 주어지는 종이를 확인 후 서로 다른 것이 확인되면
  • state를 변경 후 탈출하는 조건을 걸어 조금 더 빠르게 해결하고자 했으며
  • 만약 state가 false인 경우, 해당 종이를 9분할 하여 다시 확인하며 해결.

 

import java.io.*;
import java.util.*;
public class _1780 {
    static int[][] mtx;
    static int N;
    static int[] result = {0,0,0};

    public static void main(String[] agrs) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        N = Integer.parseInt(br.readLine());
        mtx = new int[N][N];
        for (int i=0; i<N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j=0; j<N; j++) mtx[i][j] = Integer.parseInt(st.nextToken());
        }
        reg(0,N,0,N,N);
        for (int num: result) System.out.println(num);

    }

    public static void reg(int si, int ei, int sj, int ej, int n) {
        boolean state = true;
        int now = mtx[si][sj];
        for (int i=si; i<ei; i++) {
            for (int j=sj; j<ej; j++) {
                if (mtx[i][j]!= now) {
                    state = false;
                    break;
                }
            } if (!state) break;
        }
        if (!state) {
            int t_half = n/3;
            for (int i=si; i<ei; i+=t_half) {
                for (int j=sj; j<ej; j+=t_half) reg(i,i+t_half, j,j+t_half, t_half);
            }
        } else {
            result[now + 1] += 1;
            return;
        }
    }
}