UVA297 Quadtrees

  • 题目中给出的四分树一定是“满叶”的,因此知道了先序遍历就能确定这棵树,边读入边建图即可;
  • 统计答案时同时遍历两棵树;
  • 注意在计算答案的过程中可能会出现一颗树的当前节点已经不存在的情况,同样要作为边界处理;
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn=32*32;
struct node{
    int a,b,c,d;
    char col;
};
node tree1[2055],tree2[2055];
char c;
int dat,cnt=1,ans;
void build(struct node tr[],int root)
{
    c=getchar();
    tr[root].col=c;
    if(c=='p')
    {
        tr[root].a=++cnt;
        build(tr,cnt);
        tr[root].b=++cnt;
        build(tr,cnt);
        tr[root].c=++cnt;
        build(tr,cnt);
        tr[root].d=++cnt;
        build(tr,cnt);
    }
}
void dfs(int p1,int p2,int tmp)
{
    if(tree1[p1].col=='f'||tree2[p2].col=='f'||tree1[p1].col=='e'&&tree2[p2].col=='e'||p1==0||p2==0)
    {
        ans+=((tree1[p1].col=='f'||tree2[p2].col=='f')?maxn/tmp:0);
        return ;
    }
    dfs(tree1[p1].a,tree2[p2].a,tmp*4);
    dfs(tree1[p1].b,tree2[p2].b,tmp*4);
    dfs(tree1[p1].c,tree2[p2].c,tmp*4);
    dfs(tree1[p1].d,tree2[p2].d,tmp*4);
}
void intt()
{
    for(int i=1;i<=1025;i++)
    tree1[i].a=tree1[i].b=tree1[i].c=tree1[i].d=tree2[i].a=tree2[i].b=tree2[i].c=tree2[i].d=0;
    ans=0;cnt=1;
}
int main()
{
//    freopen("1.txt","w",stdout);
    cin>>dat;
    while(dat--)
    {
        getchar();
        build(tree1,1);
        getchar();
        cnt=1;
        build(tree2,1);
        dfs(1,1,1);
        cout<<"There are "<<ans<<" black pixels."<<endl;
        intt();
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希表扁豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值