์‹ค๋ฒ„1 : ๋ถ„ํ• ์ •๋ณต ๋ฌธ์ œ์ด๋‹ค.

์ฒ˜์Œ์— ๋„ˆ๋ฌด ์‚ฝ์งˆํ–ˆ๋‹ค.. ๊ทธ๋ƒฅ ๊ตฌ์กฐ์ ์œผ๋กœ ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

Code

//
//  main.cpp
//  algorithm_prac
//
//  Created by ์ตœ์™„์‹ on 2021/04/05.
//
 
#include <iostream>
#include <vector>
using namespace std;
int N;
int map[64][64];
vector<char> ans;
 
void go(int sy, int sx, int d){
    int setN = map[sy][sx];
    int flag = 0;
    for (int i = sy; i < sy+d; i++) {
        for (int j = sx; j < sx+d; j++) {
            if (setN != map[i][j]) {
                flag = -1;
                break;
            }
        }
        if (flag == -1) break;
    }
    
    if (flag == 0) ans.push_back(setN + '0');
    else {
        ans.push_back('(');
        
        go(sy, sx, d/2);
        go(sy, sx+d/2, d/2);
        go(sy+d/2, sx, d/2);
        go(sy+d/2, sx+d/2, d/2);
        
        ans.push_back(')');
    }
}
 
int main(){
    cin >> N;
    for (int i = 0; i < N; i++) {
        char temp[64];
        cin >> temp;
        for (int j = 0; j < N; j++) {
            map[i][j] = temp[j] - '0';
        }
    }
 
    go(0, 0, N);
    for (int i = 0; i < int(ans.size()); i++) {
        cout << ans[i];
    }
    return 0;
}
 
 

Reference