[Java] 백준 1780 종이의 개수

Lee GaEun·2025년 1월 8일

[Java] 알고리즘

목록 보기
42/93

1780 종이의 개수 문제 링크

문제


#1

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

class Main {
    static int N;
    static int[][] paper;
    static int mOne, zero, one;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());

        StringTokenizer st;
        paper = new int[N][N];
        for(int i=0; i<N; i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=0; j<N; j++) {
                paper[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        countPaper(N,0, 0);
        System.out.println(mOne);
        System.out.println(zero);
        System.out.println(one);
    }

    static void countPaper(int cut, int low, int col) {
        int[] answer = new int[3];
        int w = paper[low][col];
        outerLoop:
        for(int i=low; i<low+cut; i++) {
            for(int j=col; j<col+cut; j++) {
                if(paper[i][j]==-1) answer[0]++;
                else if (paper[i][j]==0) answer[1]++;
                else if (paper[i][j]==1) answer[2]++;

                if(paper[i][j]!=w) {
                    for(int a=0; a<3; a++) {
                        for(int b=0; b<3; b++) {
                            countPaper(cut/3, low+(a*(cut/3)), col+(b*(cut/3)));
                        }
                    }
                    break outerLoop;
                }
            }
        }
        mOne += answer[0]==(cut*cut) ? 1 : 0;
        zero += answer[1]==(cut*cut) ? 1 : 0;
        one += answer[2]==(cut*cut) ? 1 : 0;
    }
}

  • 후.. 진짜 힘겹게 성공..
  • 이건 지금 <큰 구역 검사하다가 아니면 쪼개서 검사> 이런 식으로 재귀를 사용한 거

다른 사람의 풀이

import java.util.Scanner;
 
public class Main {
 
	public static int[][] board;
	public static int GRAY = 0;		// -1에 해당
	public static int WHITE = 0;	// 0에 해당
	public static int BLACK = 0;	// 1에 해당
 
	public static void main(String[] args) {
 
		Scanner in = new Scanner(System.in);
 
		int N = in.nextInt();
		board = new int[N][N];
 
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				board[i][j] = in.nextInt();
			}
		}
 
		partition(0, 0, N);
 
		System.out.println(GRAY);	// -1
		System.out.println(WHITE);	// 0
		System.out.println(BLACK);	// 1
 
	}
 
	
	public static void partition(int row, int col, int size) {
 
		// 만약 같은 색상으로 이루어져있다면 해당 색상 카운트를 증가시킨다.
		if (colorCheck(row, col, size)) {
			if(board[row][col] == -1) { 
				GRAY++;
			}
			else if(board[row][col] == 0) {
				WHITE++;
			}
			else {
				BLACK++;
			}
 
			return;
		}
 
		int newSize = size / 3;
		
		partition(row, col, newSize);								// 왼쪽 위
		partition(row, col + newSize, newSize);						// 중앙 위
		partition(row, col + 2 * newSize, newSize);					// 오른쪽 위
		
		partition(row + newSize, col, newSize);						// 왼쪽 중간
		partition(row + newSize, col + newSize, newSize);			// 중앙 중간
		partition(row + newSize, col + 2 * newSize, newSize);		// 오른쪽 중간
		
		partition(row + 2 * newSize, col, newSize);					// 왼쪽 아래
		partition(row + 2 * newSize, col + newSize, newSize);		// 중앙 아래
		partition(row + 2 * newSize, col + 2 * newSize, newSize);	// 오른쪽 아래
 
	}
 
	// 해당 영역이 같은 색인지 검사하는 메소드
	public static boolean colorCheck(int row, int col, int size) {
		int color = board[row][col];
 
		// 각 블럭의 시작점(row, col)부터 블럭의 끝(row + size, col + size)까지 검사
		for (int i = row; i < row + size; i++) {
			for (int j = col; j < col + size; j++) {
				if (color != board[i][j]) {
					return false;
				}
			}
		}
		return true;
	}
 
}
  • 뭔가 굉장히 찝찝한 성공 같아서 다른 사람 풀이를 읽어봄
  • 결국 방식은 비슷한 거 같은데
  • 더러운 수식을 빼고 참, 거짓으로 구분해서 좀 더 깔끔함
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글