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;
}

猜你喜欢

转载自blog.csdn.net/qq_40679299/article/details/80303431
今日推荐