首页 > 代码库 > UVA - 297 Quadtrees (四分树)

UVA - 297 Quadtrees (四分树)

题意:求两棵四分树合并之后黑色像素的个数。

技术分享

分析:边建树边统计。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1100 + 10;
const int MAXT = 40 + 10;
using namespace std;
char s[MAXN];
int vis[MAXT][MAXT];
int cnt;
void dfs(int &id, int r, int c, int len){
    char cc = s[id++];//传参加自增,遍历每个元素
    if(cc == p){
        dfs(id, r, c + len / 2, len / 2);//1
        dfs(id, r, c, len / 2);//2
        dfs(id, r + len / 2, c, len / 2);//3
        dfs(id, r + len / 2, c + len / 2, len / 2);//4
    }
    else if(cc == f){
        for(int i = r; i < r + len; ++i){
            for(int j = c; j < c + len; ++j){
                if(!vis[i][j]){
                    vis[i][j] = 1;
                    ++cnt;
                }
            }
        }
    }
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        memset(vis, 0, sizeof vis);
        cnt = 0;
        for(int i = 0; i < 2; ++i){
            scanf("%s", s);
            int id = 0;
            dfs(id, 0, 0, 32);
        }
        printf("There are %d black pixels.\n", cnt);
    }
    return 0;
}

 

UVA - 297 Quadtrees (四分树)