Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

dh-winternagi 님의 블로그

(1992) 쿼드트리 본문

백준 (C++)/Solve

(1992) 쿼드트리

dh-winternagi 2026. 4. 18. 11:49

https://www.acmicpc.net/problem/1992

단계별로 풀어보기

24단계(분할 정복) 2번째

 

 

 

색종이 만들기와 같은 방식으로 풀면 된다.

 

 

 

#include <iostream>
#include <vector>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  
  int n;
  
  cin >> n;
  
  vector v(n+1, vector<int> (n+1));
  
  for(int i=1;i<=n;i++){
    string s;
    
    cin >> s;
    
    for(int j=1;j<=n;j++){
      v[i][j]= s[j-1]-'0';
      v[i][j]+= v[i-1][j]+v[i][j-1]-v[i-1][j-1];
    }
  }
  
  auto func= [&](auto self, int x, int y, int l) -> string {
    int acc= v[x+l-1][y+l-1]-v[x-1][y+l-1]-v[x+l-1][y-1]+v[x-1][y-1];
    
    if(acc==0)  return "0";
    else if(acc==l*l)  return "1";
    
    return "("+
      self(self, x, y, l/2)+
      self(self, x, y+l/2, l/2)+
      self(self, x+l/2, y, l/2)+
      self(self, x+l/2, y+l/2, l/2)+
      ")";
  };
  
  cout << func(func, 1, 1, n);
  
  return 0;
}

'백준 (C++) > Solve' 카테고리의 다른 글

(1629) 곱셈  (0) 2026.04.18
(1780) 종이의 개수  (0) 2026.04.18
(2630) 색종이 만들기  (0) 2026.04.18
(13305) 주유소  (0) 2026.04.18
(1541) 잃어버린 괄호  (0) 2026.04.18