접근 방법
이전에 풀어봤던 1992번과 거의 똑같은 문제다.
백준 BOJ 1992번
접근 방법 이전에 풀었던 1780번과 유사한 문제다. 다른 점이 있다면 이전에 푼 것은 9분할, 이번 문제는 4분할 한다는 점이다. https://edvedv.tistory.com/28?category=1038364 백준 BOJ 1780번 접근 방법 이..
edvedv.tistory.com
재귀, 분할정복을 사용해서 파란색과 하얀색을 카운트해주면 쉽게 해결할 수 있다.
코드
import java.util.*;
public class Main {
public static int[][] arr;
static int count[] = {0, 0};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
arr = new int[N][N];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
arr[i][j] = in.nextInt();
}
}
QuadTree(0, 0, N);
System.out.println(count[0]);
System.out.println(count[1]);
in.close();
}
public static void QuadTree(int row, int col, int size){
if(check(row, col, size)){
if(arr[row][col] == 0) count[0]++;
else count[1]++;
return;
}
int next = size / 2;
QuadTree(row, col, next);
QuadTree(row, col + next, next);
QuadTree(row + next, col, next);
QuadTree(row + next, col + next, next);
}
// 같은 숫자인지 체크하는 메소드
public static boolean check(int row, int col, int size) {
int num = arr[row][col];
for(int i = row; i < row + size; i++){
for(int j = col; j < col + size; j++){
if(num != arr[i][j]) return false;
}
}
return true;
}
}
'알고리즘 > Class 3' 카테고리의 다른 글
백준 BOJ 5430번 - AC (0) | 2022.09.04 |
---|---|
백준 BOJ 2667번 - 단지번호붙이기 (0) | 2022.08.31 |
백준 BOJ 2606번 - 바이러스 (0) | 2022.08.30 |
백준 BOJ 2579번 - 계단 오르기 (0) | 2022.08.29 |
백준 BOJ 2178번 - 미로 탐색 (0) | 2022.08.28 |
댓글