-
[백준] 2630 색종이 만들기ALGORITHM/BOJ 2021. 7. 9. 16:41
https://www.acmicpc.net/problem/2630
2021-07-09
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main2630 {public static int N, white, blue;public static int[][] arr;public static void solve(int size, int x, int y) {if(size == 1) {if(arr[x][y] == 0) white++;else blue++;return;}if(check(size, x, y, arr[x][y])) {return;}solve(size/2, x, y);solve(size/2, x, y+size/2);solve(size/2, x+size/2, y);solve(size/2, x+size/2, y+size/2);}public static boolean check(int size, int x, int y, int color) {for(int i = x; i < x+size; i++) {for(int j = y; j < y+size; j++) {if(arr[i][j] != color) return false;}}if(color == 0) white++;else blue++;return true;}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken().trim());white = 0;blue = 0;arr = new int[N][N];for(int i = 0; i < N; i++) {st = new StringTokenizer(bf.readLine());for(int j = 0; j < N; j++) {arr[i][j] = Integer.parseInt(st.nextToken());}}solve(N, 0, 0);System.out.println(white);System.out.println(blue);}}cs #문제풀이
분할정복
색종이를 계속 4등분한다고 생각하면 된다.
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 1717 집합의 표현 (0) 2021.08.14 [백준] 1074 Z (0) 2021.07.13 [백준] 13913 숨바꼭질 4 (0) 2021.07.09 [백준] 12851 숨바꼭질 2 (0) 2021.06.27 [백준] N과 M (시리즈) (0) 2021.04.21