728x90
반응형
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int [][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
String[] s = br.readLine().split(" ");
for (int j = 0; j < n; j++) {
arr[i][j] = Integer.parseInt(s[j]);
}
}
solution(n,arr,0,0);
System.out.println(white);
System.out.println(black);
}
static int white;
static int black;
private static void solution(int size, int[][] arr,int x,int y) {
int blackOrWhite = arr[x][y];
if(size==1){
if(blackOrWhite==0){
white++;
}else{
black++;
}
return;
}
boolean check = true;
for (int i = x; i < x+size; i++) {
for (int j = y; j < y+size; j++) {
if(arr[i][j] != blackOrWhite){
check = false;
break;
}
}
if(!check) break;
}
if(check){
if(blackOrWhite==0){
white++;
}else{
black++;
}
}else{
solution(size/2,arr,x,y);
solution(size/2,arr,x+(size/2),y);
solution(size/2,arr,x,y+(size/2));
solution(size/2,arr,x+(size/2),y+(size/2));
}
}
}
728x90
반응형
'알고리즘 > DFS, BFS, 시뮬, 백트래킹' 카테고리의 다른 글
[백준] 7562번 나이트의 이동 #Java (0) | 2022.05.27 |
---|---|
[백준] 2578번 빙고 #Java (0) | 2022.05.25 |
[알고리즘] 미로탐색(DFS) with Java, 초기화 전략 (0) | 2022.04.22 |
[프로그래머스] N-Queen 문제 (0) | 2021.11.09 |
[프로그래머스] 타겟 넘버 - BFS (0) | 2021.09.20 |