• uva 297 Quadtrees


    题意:讲的是一个 1024个格子黑白图片变成树的四叉树的数据结构,每一次把区域化为4个区间,如果这个区间全为黑则 用‘f’表示,如果全为白  则用‘e’表示,如果既有黑也有白则用‘p’表示,他用字符串的形式给出两棵树,求出这两个图像相叠加所得到图形的黑块的个数;

    解题思路:最开始建树想用栈,不过没建成,用了一种自我感觉比较搓的递归建树,然后同时深搜找两棵树的最大黑节点就可以了。。

    解题代码:

    // File Name: uva297.c
    // Author: darkdream
    // Created Time: 2013年05月20日 星期一 11时03分48秒
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    #include <unistd.h>
    char str[2000];
    struct node
    {
        char c;
        struct node * next[4];
    };
    struct node * newnode()
    {
        struct node *p; 
        p = (node*)malloc(sizeof(node));
        for(int i = 0 ;i < 4 ;i ++)
        {
            p->next[i] = NULL;
        }
        return p;
    }
    int j = 1;
    int sum = 0 ;
    void findnode(struct node *p)
    {
        struct node *temp;
        int k = 0;
        while(k < 4)
        {
            if(str[j] != 'p')
            {
    
                temp = newnode();
                temp->c = str[j];
                p->next[k] = temp;
                j++;
                k++;
            }
            else
            {
                temp = newnode();
                temp->c = str[j];
                p->next[k] = temp;
                j++;
                findnode(p->next[k]);
                k++;
            }
    
        }
        return;
    }
    void findalong(struct node *p,int stemp)
    {
        if(p->c == 'f')
        {
            sum += stemp;
            return;
        }
        if(p->next[0] != NULL)
        for(int i =0 ; i< 4; i ++)
            findalong(p->next[i],stemp/4);
    
    }
    void finddouble (struct node *p1,struct node *p2,int stemp)
    {
        if(p1->c == 'f' || p2->c == 'f')
        {
            sum += stemp;
            return;
        }
        if(p1->c == 'e' && p2->c == 'e')
            return;
        if(p1->c == 'p' && p2->c == 'p')
        {
            for(int i = 0 ;i < 4 ;i ++)
                finddouble(p1->next[i],p2->next[i],stemp/4);
            return;
        }
        if(p1->c == 'e')
            findalong(p2,stemp);
        else
            findalong(p1,stemp);
    }
    void print(struct node *p)
    {
        if(p->next[0] != NULL)
        {  printf("%c",p->c);
            for(int i = 0 ;i < 4; i ++)
                print(p->next[i]);
        }
    }
    int main(){
    
        //freopen("/home/plac/problem/input.txt","r",stdin);
        //freopen("/home/plac/problem/output.txt","w",stdout);
        int n;
        scanf("%d",&n);
        getchar();
        while(n--)
        {
            struct node *head1,*head2,*p;
            j = 1;
            sum = 0 ;
            scanf("%s",str);
            head1 = newnode();
            head1->c = str[0];
            if(head1->c == 'p')
                findnode(head1);
            scanf("%s",str);
    
            j = 1;
            head2 = newnode();
            head2->c = str[0];
            if(head2->c == 'p')    
                findnode(head2);
             finddouble(head1,head2,1024);     
            //print(head1);
            //print(head2);
            printf("There are %d black pixels.\n",sum);
    
        }
        return 0 ;
    }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    常见错误3——错误2 error C2143: 语法错误 : 缺少“;”(在“}”的前面)
    MFC DLL封装窗体详细步骤
    指针函数与函数指针的区别
    DOM
    BOM
    JS动态创建元素
    JS节点
    事件和节点部分总结
    DOM基础
    自定义对象
  • 原文地址:https://www.cnblogs.com/zyue/p/3090218.html
Copyright © 2020-2023  润新知