import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_2630 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int divi = N/2;
int[][] confetti = new int[N][N];
int blue = 0;
int cutting, count = 0;
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
confetti[i][j] = Integer.parseInt(st.nextToken());
}
}
while (divi > 1) {
for(int i=0; i<divi; i++) {
blue = 0;
for(int j=0; j<divi; j++) {
if(confetti[i][j] == 1) {
++blue;
}
}
for(int j=divi; j<N-1; j++) {
if(confetti[i][j] == 1) {
++blue;
}
}
blue = 0;
if(blue == divi){
++count;
}
}
for(int i=divi; i<N-1; i++) {
blue = 0;
for(int j=0; j<divi; j++) {
if(confetti[i][j] == 1) {
++blue;
}
}
blue = 0;
for(int j=divi/2; j<N-1; j++) {
if(confetti[i][j] == 1) {
++blue;
}
}
if(blue == divi){
++count;
}
}
divi /= 2;
}
System.out.println(count);
}
}
안되는 이유
package knou.Divid_and_conquer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_2630 {
public static int white = 0;
public static int blue = 0;
public static int[][] colorPaper;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
colorPaper = new int[N][N];
StringTokenizer st;
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++) {
colorPaper[i][j] = Integer.parseInt(st.nextToken());
}
}
Partition(0, 0, N);
System.out.println(white);
System.out.println(blue);
}
public static void Partition(int row, int col, int size) {
if(colorCheck(row, col, size)) {
if(colorPaper[row][col] == 0) {
white++;
} else {
blue++;
}
return;
}
int newSize = size/2;
Partition(row, col, newSize); // 2사분면
Partition(row, col+newSize, newSize); // 1사분면
Partition(row+newSize, col, newSize); // 3사분면
Partition(row+newSize, col+newSize, newSize); // 4사분면
}
public static boolean colorCheck(int row, int col, int size) {
int color = colorPaper[row][col];
for(int i=row; i<row+size; i++) {
for(int j=col; j<col+size; j++) {
if(colorPaper[i][j] != color) {
return false;
}
}
}
return true;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_2630 {
public static int white = 0;
public static int blue = 0;
public static int [][] colorPaper;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int size = Integer.parseInt(st.nextToken());
colorPaper = new int[size][size];
for(int i=0; i<size; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<size; j++){
colorPaper[i][j] = Integer.parseInt(st.nextToken());
}
}
Partition(0, 0, );
System.out.println(white);
System.out.println(blue);
}
}
이로 인해 저번 코드에 대한 이해가 부족하다고 판단, 따라서 코드리뷰 작성해보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_2630 {
// 하얀색 색종이를 자른 횟수를 저장할 변수
public static int white = 0;
// 푸른색 색종이의 개수를 저장할 변수
public static int blue = 0;
// 전체 색종이를 입력받을 색종이 판
public static int [][] colorPaper;
public static void main(String[] args) throws IOException {
// 한 줄씩 입력받기 위한 BufferedReader 클래스 객체 선언
// 문자열 파싱을 위한 StringTokenizer 클래스 객체 선언
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
// 전체 배열의 크기
int size = Integer.parseInt(st.nextToken());
// 전체 배열의 크기만큼 색종이를 만들어준다.
colorPaper = new int[size][size];
// 배열에 색종이들을 입력하는 반복문
for(int i=0; i<size; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<size; j++){
colorPaper[i][j] = Integer.parseInt(st.nextToken());
}
}
// 구역을 나누기 위한 함수 호출
Partition(0, 0, size);
// 답 출력
System.out.println(white);
System.out.println(blue);
}
public static void Partition(int row, int col, int size) {
// Colorcheck 함수 호출 - 색종이의 크기를 판별함
if(ColorCheck(row, col, size)) {
// 만약에 색종이의 색깔이 하얀색이라면
if(colorPaper[row][col] == 0) {
// 하얀색 색종이의 개수를 늘려줌
white++;
} else {
// 아니라면 파란 색종이이므로 파란 색종이를 올려줌
blue++;
}
// 재귀함수를 사용하니까 함수가 끝나는 부분 기록
return;
}
// N/2씩 줄어드는 색종이의 크기 설정
int newSize = size/2;
// 사분면은 시계 반대방향으로 돌아감.
// 시계방향대로 세어보면 2 -> 1 -> 4 -> 3
Partition(row, col, newSize); // 2사분면
Partition(row,col+newSize, newSize); // 1사분면
Partition(row+newSize, col, newSize); // 3사분면
Partition(row+newSize, col+newSize, newSize); //4사분면
}
public static boolean ColorCheck(int row, int col, int size) {
// 첫 번째 원소 기준
int color = colorPaper[row][col];
// 잘려진 부분의 색 체크
for(int i=row; i<row+size; i++){
for(int j=col; j<col+size; j++){
// 만약에 색종이와 지정된 색깔이 다르면 false를 리턴함
if(colorPaper[i][j] != color) {
return false;
}
}
}
// 검사가 모두 통과했다는 의미이므로 true 리턴
return true;
}
}
import java.util.*;
import java.io.*;
public class Main {
public static int[][] colorPaper;
public static int white;
public static int blue;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int size = Integer.parseInt(st.nextToken());
colorPaper = new int[size][size];
for(int i=0; i<size; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<size; j++) {
colorPaper[i][j] = Integer.parseInt(st.nextToken());
}
}
Partition(0, 0, size);
System.out.println(white);
System.out.println(blue);
}
public static void Partition(int row, int col, int size) {
if(CheckColor(row, col, size)){
if(/*colorPaper[col][row]*/ colorPaper[col][row] == 0){
white++;
} else {
blue++;
}
return;
}
int newSize = size/2;
Partition(row, col, newSize); // 2사분면
Partition(row, col+newSize, newSize); // 1사분면
Partition(row+newSize, col, newSize); // 3사분면
Partition(row+newSize, col+newSize, newSize); // 4사분면
}
public static boolean CheckColor(int row, int col, int size){
int color = colorPaper[row][col];
for(/*int i=0; i<size; i++*/ int i=row; i<row+size; i++) {
for(/*int j=0; j<size; j++*/ int j=col; j<col+size; j++) {
if(color != colorPaper[i][j]) {
return false;
}
}
}
return true;
}
}