第一道非二叉树题

来源:互联网 发布:如何编写python程序 编辑:程序博客网 时间:2024/05/01 16:55

例题6 -11四分数


#include<cstdio>
#include<iostream>
#include<cstring>


const int len=32;
const int maxn=1024+10;
char s[maxn];
int buf[len][len],cnt;


//把字符串s[p...]导出到以(r,c)为左上角坐标,w为边长的正方形区域中
//2 1
//3 4


void draw(const char* s,int& p,int r,int c,int w)
{
    char ch=s[p++];
    if(ch=='p')
    {
        draw(s,p,r,c+w/2,w/2);
        draw(s,p,r,c,w/2);
        draw(s,p,r+w/2,c,w/2);
        draw(s,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 T;
    scanf("%d",&T);
    while(T--)
    {
        memset(buf,0,sizeof(buf));
        cnt=0;
        for(int i=0;i<2;i++)
        {
            scanf("%s",s);
            int p=0;
            draw(s,p,0,0,len);
        }
        printf("There are %d black pixels.\n",cnt);
    }
    return 0;
}




//递归十分巧妙,要习惯这种奇妙的递归方式

0 0
原创粉丝点击