자바/알고리즘 문제 풀이

백준/1780 종이의 갯수 / 부분분할, 재귀

backend dev 2022. 12. 24.

종이의 개수

 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 256 MB 34181 20253 15205 58.503%

문제

N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1 중 하나가 저장되어 있다. 우리는 이 행렬을 다음과 같은 규칙에 따라 적절한 크기로 자르려고 한다.

  1. 만약 종이가 모두 같은 수로 되어 있다면 이 종이를 그대로 사용한다.
  2. (1)이 아닌 경우에는 종이를 같은 크기의 종이 9개로 자르고, 각각의 잘린 종이에 대해서 (1)의 과정을 반복한다.

이와 같이 종이를 잘랐을 때, -1로만 채워진 종이의 개수, 0으로만 채워진 종이의 개수, 1로만 채워진 종이의 개수를 구해내는 프로그램을 작성하시오.

입력

첫째 줄에 N(1 ≤ N ≤ 37, N은 3k 꼴)이 주어진다. 다음 N개의 줄에는 N개의 정수로 행렬이 주어진다.

출력

첫째 줄에 -1로만 채워진 종이의 개수를, 둘째 줄에 0으로만 채워진 종이의 개수를, 셋째 줄에 1로만 채워진 종이의 개수를 출력한다.

예제 입력 1 복사

9
0 0 0 1 1 1 -1 -1 -1
0 0 0 1 1 1 -1 -1 -1
0 0 0 1 1 1 -1 -1 -1
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
0 1 -1 0 1 -1 0 1 -1
0 -1 1 0 1 -1 0 1 -1
0 1 -1 1 0 -1 0 1 -1

예제 출력 1 복사

10
12
11

 


풀이

차분하게 분할하는방법을 생각하고,

인덱스를 헷갈리지않게 조심한다.

 

 

import java.io.*;
import java.util.Arrays;


public class Main {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));


    /*

     */

    static int n;
    static int[][] paper  ;
    static int result1=0;
    static int result0=0;
    static int resultMinus=0;

    public static void main(String[] args) throws IOException {

        input();
        solve();

        bw.flush();
        bw.close();

    }

    static void input() throws IOException {
        n = Integer.parseInt(br.readLine());
        paper = new int[n][n];
        for (int i = 0; i < n; i++) {
            int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt)
                .toArray();
            paper[i] = input;
        }


    }

    static void solve() throws IOException {
        dividePaper(0, 0, n);
        bw.write(resultMinus+"\n"+result0+"\n"+result1);

    }

    static void dividePaper (int x, int y ,int n)throws IOException {
        if (checkPaper(x, y, n)) {
            int firstNumber = paper[x][y];
            if (firstNumber == 1) {
                result1++;
            } else if (firstNumber == 0) {
                result0++;
            } else {
                resultMinus++;
            }
            return;
        }

        //왼쪽 맨위부터   [x가 i 인덱스이므로 -> 행 (가로) , y가 j 인덱스이므로 -> 열(세로)]
        dividePaper(x, y, n / 3); // 1번
        dividePaper(x, y+(n/3), n / 3); // 2번
        dividePaper(x, y+(2*n/3), n / 3); // 3번
        dividePaper(x+(n/3), y, n / 3); // 4번
        dividePaper(x+(n/3), y+(n/3), n / 3); // 5번
        dividePaper(x+(n/3), y+(2*n/3), n / 3); // 6번
        dividePaper(x+(2*n/3), y, n / 3); // 7번
        dividePaper(x+(2*n/3), y+(n/3), n / 3); // 8번
        dividePaper(x+(2*n/3), y+(2*n/3), n / 3); // 9번

    }

    static boolean checkPaper (int x, int y , int n) throws IOException {
        int startNumber = paper[x][y];
        for (int i = x; i < x + n; i++) {
            for (int j = y; j < y + n; j++) {
                if (paper[i][j] != startNumber) {
                    return false;
                }
            }
        }
        return true;
    }
    
}

 

댓글