uva 297

一个四叉树的题,建树遍历都没啥难度,就是遍历的时候要处理一个矩阵比较麻烦,折腾半天才把代码搞的简洁了点,虽然a了感觉还是不对劲,看了别人的代码才想起来要求的东西跟矩阵象限啥的毛线关系都没有,不用解码直接在树上算就行。按这个思路重新写一部分就简单多了,不过很奇怪运行时间竟然比第一种慢。
第二种方法
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
    int n;
    node *son[4];
    node(int a){ n=a; }
};
int depth[]={1024,256,64,16,4,1};
int cnt;
int input(){
    char c=getchar();
    if(c=='p') return 2;
    if(c=='e') return 0; return 1;
}
void build(node *head){
    for(int i=0;i<4;i++){
        node *p=new node(input());
        head->son[i]=p;
        if(p->n==2) build(p);
    }
}
void fun(node *p,node *q,int d){
    if(p->n==1||q->n==1){
        cnt+=depth[d]; return;
    }
    if(p->n==0&&q->n==0) return;
    node *a=p,*b=q;
    for(int i=0;i<4;i++){
        if(p->n==2) a=p->son[i];
        if(q->n==2) b=q->son[i];
        fun(a,b,d+1);
    }
}
int main()
{
    int t; cin>>t; getchar();
    while(t--){
        cnt=0;
        node *h1,*h2;
        h1=new node(input());
        if(h1->n==2) build(h1); getchar();
        h2=new node(input());
        if(h2->n==2) build(h2); getchar();
        fun(h1,h2,0);
        printf("There are %d black pixels.\n",cnt);
    }
    return 0;
}


第一种方法,等于是把图像恢复出来了,虽然做这题用不到,不过这个过程还是有点意思的,坐标变来变去比较麻烦,估计实际中有更好的方法。

#include <iostream>
#include<cstdio>
#include<cstring>
#define X 0
#define Y 2
#define cal(a) (rg[a+1]-rg[a]+1)/2
using namespace std;
struct node{
    int n;
    node *son,*bro;
    node(int a){ son=NULL; bro=NULL; n=a; }
};
int img[35][35];
int dir[4][4]={{1,0,1,0},{-1,-1,0,0},{0,0,-1,-1},{1,1,0,0}};
int input(){
    char c=getchar();
    if(c=='p') return 2;
    if(c=='e') return 0; return 1;
}
void build(node *head){
    node *p=new node(input());
    head->son=p;
    if(p->n==2) build(p);
    for(int i=0;i<3;i++){
        node *q=new node(input());
        p->bro=q; p=q;
        if(p->n==2) build(p);
    }
}
void fun(node *p,int range[]){
    if(p->n==1){
        for(int i=range[X];i<=range[X+1];i++)
            for(int j=range[Y];j<=range[Y+1];j++)
                img[i][j]=1;
        return;
    }
    if(p->n==0) return;
    p=p->son;
    int rg[4]; for(int i=0;i<4;i++) rg[i]=range[i];
    int cg[4]; cg[X]=cg[X+1]=cal(X); cg[Y]=cg[Y+1]=cal(Y);
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++) rg[j]+=dir[i][j]*cg[j];
        fun(p,rg);
        p=p->bro;
    }
}
int main()
{
    int t; cin>>t; getchar();
    while(t--){
        memset(img,0,sizeof(img));
        node *h1,*h2;
        h1=new node(input());
        if(h1->n==2) build(h1); getchar();
        h2=new node(input());
        if(h2->n==2) build(h2); getchar();
        int rg[4]={1,32,1,32}; //x1x2y1y2
        fun(h1,rg); fun(h2,rg);
        int cnt=0;
        for(int i=1;i<=32;i++)
            for(int j=0;j<=32;j++)
                if(img[i][j]) cnt++;
        printf("There are %d black pixels.\n",cnt);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值