์‹ค๋ฒ„1 : ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

๋„ˆ๋ฌด ์งœ์ฆ๋‚œ๋‹ค. ๋ฌธ์ œ ์ œ๋Œ€๋กœ ์ฝ์–ด. ์ œ๋ฐœ์ œ๋ฐœ์ ญ๋ผ..

  1. ๋ฑ€์ด ์ง€๋‚˜๊ฐ€๋Š” ๊ฒฝ๋กœ๋ฅผ ์žก์•„์ค„ ์ž๋ฃŒ๊ตฌ์กฐ
    1. map์— ํ‘œ์‹œํ•ด์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.
    2. tail์˜ ์œ„์น˜๋ฅผ trackingํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. โ†’ queue
  2. ์‹œ๊ฐ„
  3. ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” ๋ฐฉํ–ฅ
  4. ๋ฐฉํ–ฅ์— ๋”ฐ๋ฅธ ์šฐํšŒ์ „ ์ขŒํšŒ์ „
  5. action์„ ์•Œ๋ ค์ค„ ๋ฐฉํ–ฅ

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