UVA - 297 Quadtrees (四分树)

Posted SomnusMistletoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (四分树)的主要内容,如果未能解决你的问题,请参考以下文章

UVA297:Quadtrees(四分树)

Quadtrees UVA - 297

Uva 297 四分树

四分树- UVa 297

UVa 806 四分树

四分树(Quadtrees)