# Uva 297 - Quadtrees --- # 題目大意 用四元樹表達一個只有黑白色、32*32的圖案,給兩棵樹的前序表達式,要求輸出兩圖重疊後總共有幾格黑色。 --- # 題解 開一個陣列,用遞迴塗色,最後直接數有幾個是黑色的。 --- ```=c++ #include <bits/stdc++.h> #define ll long long #define pb push_back #define pf push_front #define ft first #define sec second #define pr pair<int,int> #define ISCC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; int t ,n ,m ,ans ,g[35][35] ,pos; string s; void sol(int x ,int y ,int len) { pos++; if(!len || pos >= s.size()) return; //cout << s[pos]; if(s[pos] == 'f') { for(int i=x ;i < x+len ;i++) for(int j=y ;j < y+len ;j++) g[i][j] = 1; } else if(s[pos] == 'p') { sol(x+len/2 ,y ,len/2); sol(x ,y ,len/2); sol(x ,y+len/2 ,len/2); sol(x+len/2 ,y+len/2 ,len/2); } } int main() { cin >> t; while(t--) { memset(g,0,sizeof(g)); pos = -1; ans = 0; cin >> s; sol(1 ,1 ,32); cin >> s; pos = -1; sol(1 ,1 ,32); for(int i=1 ;i<=32 ;i++) for(int j=1 ;j<=32 ;j++) ans += g[i][j]; printf("There are %d black pixels.\n" ,ans); } return 0; } ```