[BOJ]1992 쿼드트리

강동현·2023년 12월 26일
0

코딩테스트

목록 보기
51/111

sol1: 재귀 풀이

  • 4갈래로 나뉘어 탐색
  • 범위 탐색 수행 중, 시작점의 값과 다른 값이 발견되면, 탐색 종료
#include <iostream>
#include <vector>
using namespace std;
vector<vector<char>> board(65, vector<char>(65, ' '));
void recursion(int x, int y, int n) {
    if (n == 1) {
        cout << board[x][y];
        return;
    }
    bool isSame = true;
    char firstPixel = board[x][y];
    for (int i = x; i < x + n; ++i) {
        for (int j = y; j < y + n; ++j) {
            if (board[i][j] != firstPixel) {
                isSame = false;
                break;
            }
        }
        if (!isSame) {
            break;
        }
    }
    if (isSame) {
        cout << firstPixel;
    } else {
        cout << "(";
        int half = n / 2;
        recursion(x, y, half);
        recursion(x, y + half, half);
        recursion(x + half, y, half);
        recursion(x + half, y + half, half);
        cout << ")";
    }
}
int main() {
    int N;
    cin >> N;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> board[i][j];
        }
    }
    recursion(0, 0, N);
    return 0;
}
profile
GAME DESIGN & CLIENT PROGRAMMING

0개의 댓글