ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1992번 쿼드트리
    PS 2021. 4. 4. 23:29

    www.acmicpc.net/problem/1992

     

     

     

     

    <접근방법>

    탐색하고 분할하고.

    분할1을(를) 탐색하고

    분할2을(를) 탐색하고

    분할3을(를) 탐색하고

    분할4을(를) 탐색하고

    ...

     


     

     

     

    <코드>

    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    #define MAX 64
    
    int n;
    int map[MAX][MAX];
    
    string retZip(int y, int x, int size) {
    	if (size == 1) {
    		return to_string(map[y][x]);
    	}
    
    	bool zero = true, one = true;
    	for (int i = y; i < y + size; i++) {
    		for (int j = x; j < x + size; j++) {
    			if (map[i][j]) {
    				zero = false;
    			}
    			else {
    				one = false;
    			}
    		}
    	}
    
    	string upLeft;
    	string upRight;
    	string downLeft;
    	string downRight;
    
    
    	if (zero) {
    		return to_string(0);
    	}
    	else if (one) {
    		return to_string(1);
    	}
    	else {
    		upLeft = retZip(y, x, size / 2);
    		upRight = retZip(y, x + size / 2, size / 2);
    		downLeft = retZip(y + size / 2, x, size / 2);
    		downRight = retZip(y + size / 2, x + size / 2, size / 2);
    	}
    
    	return "(" + upLeft + upRight + downLeft + downRight + ")";
    }
    
    int main() {
    	cin >> n;
    
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			scanf("%1d", &map[i][j]);
    		}
    	}
    
    	cout << retZip(0, 0, n) << '\n';
    
    	return 0;
    }

     

     

     

     

    'PS' 카테고리의 다른 글

    백준 17478 재귀함수가 뭔가요?  (0) 2021.04.06
    백준 1629 곱셈  (0) 2021.04.06
    백준 11729번 하노이 탑 이동순서  (0) 2021.04.02
    백준 1074 Z  (0) 2021.04.02
    백준 1175번 배달  (0) 2021.03.29
Designed by Tistory.