UVa297 Quadtrees

// UVa297 Quadtrees

// 题意:给两棵四分树的先序遍历,求二者合并之后(黑色部分合并)黑色像素的个数。p表示中间结点,f表示黑色(full),e表示白色(empty)
// 算法:先建树,然后统计

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int N=1024+10;
char s[N];

char buf[32][32];
int cnt;
int p;

/*
 2 1
 3 4
*/
void draw(int l, int t, int size)
{
        char ch=s[p++];
//      printf("%d %d %d %c\n", l, t, size, ch);
        if(ch=='p')
        {
                draw(l+size/2, t, size/2);
                draw(l, t, size/2);
                draw(l, t+size/2, size/2);
                draw(l+size/2, t+size/2, size/2);
        }
        else if(ch=='f')
        {
                for(int i=0;i<size;i++)
                        for(int j=0;j<size;j++)
                        {
                                if(buf[i+t][j+l]==0)
                                {
                                        buf[i+t][j+l]=1; cnt++;
                                }
                        }
        }
}

int main()
{
        int T;
        scanf("%d", &T);
        while(T--)
        {
                cnt=0;
                memset(buf, 0, sizeof(buf));
                for(int i=0;i<2;i++)
                {
                        scanf("%s", s);
                        p=0;
                        draw(0, 0, 32);
                }
                printf("There are %d black pixels.\n", cnt);
        }

    return 0;
}

posted on 2014-03-31 16:34  katago  阅读(235)  评论(0编辑  收藏  举报