UVA 297 Qudetree 四叉树(两种方法)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<sstream>
using namespace std;

const int maxn=1034;
const int len=32;

char s[maxn];
int buf[len][len];
int cnt,p;

void dfs(int k,int lx,int ly,int rx,int ry)
{
    char ch=s[p++];
    if(ch=='p'){
        int mx=(lx+rx)>>1;
        int my=(ly+ry)>>1;
        dfs(4*k,lx,ly,mx,my);
        dfs(4*k+1,mx+1,ly,rx,my);
        dfs(4*k+2,lx,my+1,mx,ry);
        dfs(4*k+3,mx+1,my+1,rx,ry);
    }
    else if(ch=='f'){
        for(int i=ly;i<=ry;i++)
            for(int j=lx;j<=rx;j++)
                if(buf[i][j]==0){
                    buf[i][j]=1;
                    cnt++;
                }
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        cnt=0;
        memset(buf,0,sizeof(buf));
        for(int i=0;i<2;i++)
        {
            p=0;
            scanf("%s",s);
            dfs(1,0,0,len-1,len-1);
        }
        printf("There are %d black pixels.\n",cnt);
    }
    return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<sstream>
using namespace std;

const int maxn=1034;
const int len=32;

char s[maxn];
int buf[len][len];
int cnt;

void dfs(int &p,int r,int c,int w)
{
    char ch=s[p++];
    if(ch=='p'){
        dfs(p,r,c,w/2);
        dfs(p,r,c+w/2,w/2);
        dfs(p,r+w/2,c,w/2);
        dfs(p,r+w/2,c+w/2,w/2);
    }
    else if(ch=='f'){
        for(int i=r;i<r+w;i++)
            for(int j=c;j<c+w;j++)
                if(buf[i][j]==0){
                    buf[i][j]=1;
                    cnt++;
                }
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        cnt=0;
        memset(buf,0,sizeof(buf));
        for(int i=0;i<2;i++)
        {
            int p=0;
            scanf("%s",s);
            dfs(p,0,0,len);
        }
        printf("There are %d black pixels.\n",cnt);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值