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;
public static int WHITE = 0;
public static int BLACK = 0;
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);
System.out.println(WHITE);
System.out.println(BLACK);
}
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];
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;
}
}
- 뭔가 굉장히 찝찝한 성공 같아서 다른 사람 풀이를 읽어봄
- 결국 방식은 비슷한 거 같은데
- 더러운 수식을 빼고 참, 거짓으로 구분해서 좀 더 깔끔함