변명은 만개 결과는 한개

[백준 2630] 색종이 만들기 (java) 본문

공부/Problem Solving

[백준 2630] 색종이 만들기 (java)

노마십가 2022. 5. 3. 01:48
728x90
반응형

2630번: 색종이 만들기 (acmicpc.net)

 

2630번: 색종이 만들기

첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.

www.acmicpc.net

import java.io.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;

public class Main {
    static int white = 0;
    static int blue = 0;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        // input Matrix
        int N = Integer.parseInt(br.readLine());
        String[][] matrix = new String[N][N];
        for (int i = 0; i < N; i++) {
            String[] str = br.readLine().split(" ");
            for (int j = 0; j < N; j++) {
                matrix[i][j] = str[j];
            }
        }

        // check input Matrix
//        for (int i = 0; i < N; i++) {
//            for (int j = 0; j < N; j++) {
//                System.out.print(matrix[i][j]);
//            }
//            System.out.println();
//        }

        if(!check(matrix)){
            isInputMatrixAllWhiteOrBlue(matrix, N);
        }


//        System.out.println("B:"+blue+","+"W:"+white);
        System.out.println(white);
        System.out.println(blue);
    }

    // 1. 모두 0 혹은 1 인지 체크하는 함수
    static void isInputMatrixAllWhiteOrBlue(String[][] inputMatrix, int N) {
        int currentN = N / 2;

        String[][] matA = new String[currentN][currentN];
        for (int i = 0; i < currentN; i++) {    // y
            for (int j = 0; j < currentN; j++) {    // x
                matA[i][j] = inputMatrix[i][j];
            }
        }
        if (!check(matA)) { // input Matrix 가 All blue 혹은 white 가 아니라면
            isInputMatrixAllWhiteOrBlue(matA, currentN);    // 반-반 사이즈로 input Matrix 넣음.
        }

        String[][] matB = new String[currentN][currentN];
        for (int i = 0; i < currentN; i++) {   // y
            for (int j = 0; j < currentN; j++) {    // x
                matB[i][j] = inputMatrix[i][currentN + j];
            }
        }
        if (!check(matB)) { // input Matrix 가 All blue 혹은 white 가 아니라면
            isInputMatrixAllWhiteOrBlue(matB, currentN);    // 반-반 사이즈로 input Matrix 넣음.
        }

        String[][] matC = new String[currentN][currentN];
        for (int i = 0; i < currentN; i++) {   // y
            for (int j = 0; j < currentN; j++) {    // x
                matC[i][j] = inputMatrix[currentN + i][j];
            }
        }
        if (!check(matC)) { // input Matrix 가 All blue 혹은 white 가 아니라면
            isInputMatrixAllWhiteOrBlue(matC, currentN);    // 반-반 사이즈로 input Matrix 넣음.
        }

        String[][] matD = new String[currentN][currentN];
        for (int i = 0; i < currentN; i++) {   // y
            for (int j = 0; j < currentN; j++) {    // x
                matD[i][j] = inputMatrix[currentN + i][currentN + j];
            }
        }
        if (!check(matD)) { // input Matrix 가 All blue 혹은 white 가 아니라면
            isInputMatrixAllWhiteOrBlue(matD, currentN);    // 반-반 사이즈로 input Matrix 넣음.
        }

        // return 은 True or False
    }

    static boolean check(String[][] inputMatrix) {
        if (Arrays.stream(inputMatrix)
                .flatMap(new Function<String[], Stream<?>>() {
                    @Override
                    public Stream<?> apply(String[] strings) {
                        return Arrays.stream(strings);
                    }
                }).allMatch(x -> x.equals("0"))) {
//            System.out.println("All 0");
            white++;
            return true;
        } else if ((Arrays.stream(inputMatrix)
                .flatMap(new Function<String[], Stream<?>>() {
                    @Override
                    public Stream<?> apply(String[] strings) {
                        return Arrays.stream(strings);
                    }
                }).allMatch(x -> x.equals("1")))) {
//            System.out.println("All 1");
            blue++;
            return true;
        } else {
//            System.out.println("complex");
            return false;
        }
    }
    // 2.
}

 

쓸데없이 반복되는거 함수로 뺄수있는데 일단 pass니까..

728x90
반응형