Skip to content

Commit

Permalink
#13 2630번 색종이 만들기
Browse files Browse the repository at this point in the history
  • Loading branch information
leejw-lu committed Nov 20, 2023
1 parent 20d56ed commit 9545359
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions 분할정복/2630_색종이 만들기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package 분할정복;
import java.util.*;
import java.io.*;

public class Main {
static int[][] graph;
static int white=0;
static int blue=0;

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

graph= 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++)
graph[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(graph[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=graph[row][col];

for (int i=row;i<row+size;i++){
for (int j=col;j<col+size;j++){
if (graph[i][j] != color)
return false;
}
}
return true;
}

}

0 comments on commit 9545359

Please sign in to comment.