UVA 11941 四叉树加输入处理

啊啊啊啊啊啊啊啊啊啊阿啊啊啊啊啊啊啊

24小时搞懂四叉树加上写出第一份四叉树,好开心

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn=1024;
const int maxm=32767;
char g[maxn][maxn];
char node[maxn*maxn];
char line[maxm];
int cnt;
int build(int k,int lx,int ly,int rx,int ry)
{
    if(lx==rx&&ly==ry)
       return node[k]=(1<<g[lx][ly]);
    int mx=(lx+rx)/2,my=(ly+ry)/2;
    int v=0;
    v|=build(k*4+1,lx,ly,mx,my);
    v|=build(k*4+2,mx+1,ly,rx,my);
    v|=build(k*4+3,lx,my+1,mx,ry);
    v|=build(k*4+4,mx+1,my+1,rx,ry);
    return node[k]=v;
}
void dfs(int u)
{
    if(node[u]==3)
    {
        if(u>0) putchar('*');
        dfs(u*4+1);
        dfs(u*4+2);
        dfs(u*4+3);
        dfs(u*4+4);
    }
    else
    {
        if(node[u]==1)
            putchar('0');
        else
            putchar('1');
    }
}
int count(int n)
{
    int res=0;
    while(n)
    {
        if(n&1)
            res++;
        n/=2;
    }
    return res;
}
int solve()
{
    int i,res=0;
    for(i=cnt;line[i];i++)
    {
        if(line[i]<'0'||line[i]>'9')
        {
            if(res)
            {
                cnt=i;
                break;
            }
        }
        else
        {
            res*=10;
            res+=(line[i]-'0');
        }
    }
    if(line[i]==0)
        cnt=i;
    if(res==0)
        return -1;
    return res;
}
int main()
{
    int t,n,sx,sy,ex,ey;
    scanf("%d\n",&t);
    while(t--)
    {
        gets(line);
        cnt=0;
        n=solve();
        if(count(n)!=1||n<=1)
        {
            puts("Size is invalid");
            continue;
        }
        memset(g,0,sizeof(g));
        while((sx=solve())!=-1)
        {
            sy=solve();
            ex=solve();
            ey=solve();
            for(int i=sx;i<=ex;i++)
                for(int j=sy;j<=ey;j++)
                    g[i][j]=1;
        }
        build(0,1,1,n,n);
        dfs(0);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值