합리적 낙관주의자

Baekjoon 1992번: 쿼드트리 (분할정복) 본문

Computer Thinking 🌟/Algorithm 📝

Baekjoon 1992번: 쿼드트리 (분할정복)

sroa.chin 2020. 10. 28. 23:56

www.acmicpc.net/problem/1992

 

1992번: 쿼드트리

첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1≤N ≤64의 범위를 가진다. 두 번째 줄부터는 길이 N 의 문자열이 N 개 들어온다. 각 문자열은 0 또는

www.acmicpc.net


모두 같은 수가 아닐경우, 모두 같은 숫자가 존재할때까지 정사각형을 4등분해서 확인하는 분할문제

 

처음부터 이차원 행렬이 모두 같은 수 일때, ( )없이 숫자 하나만 나올 수 있도록 하는 부분에서 버벅거렸다. 문제를 제대로 잘 읽어야함.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//1992번 쿼드트리
public class Main {
	static char[][] map;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		map = new char [N][N];
		
		boolean flag = true;
		for (int i = 0; i < N; i++) {
			String tmp = br.readLine();
			for (int j = 0; j < tmp.length(); j++) {
				map[i][j] = tmp.charAt(j);
				if(map[i][j] != map[0][0]) flag = false;
			}
		}//end of for
		
		if (!flag) {
			System.out.print("(");
			check(0, 0, N / 2);
			check(0, N / 2, N / 2);
			check(N / 2 , 0, N / 2);
			check(N / 2 , N / 2, N / 2);
			System.out.print(")");
		} else {
			System.out.println(map[0][0]);
		}
	}//end of main
	
	// 모두 같은 원소인지 확인하는 메소드
	// x,y 시작위치 size 정사각형 사이즈
	public static void check(int x, int y, int size) {
		if(size == 1) {
			System.out.print(map[x][y]);
			return;
		}
		char fix = map[x][y];
		
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (map[x+i][y+j] == fix)	continue;

				System.out.print("(");
				check(x, y, size / 2);
				check(x, y + size / 2, size / 2);
				check(x + size / 2, y, size / 2);
				check(x + size / 2, y + size / 2, size / 2);
				System.out.print(")");

				return;
			}
		}
		
		System.out.print(map[x][y]);
	}//end of solution
	
	
/*
8
11110000
11110000
00011100
00011100
11110000
11110000
11110011
11110011

8
00000000
00000000
00001111
00001111
00011111
00111111
00111111
00111111

8
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000

8
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111

*/
	
	
}//end of class