博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 297 - Quadtrees
阅读量:4677 次
发布时间:2019-06-09

本文共 2186 字,大约阅读时间需要 7 分钟。

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

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

解题代码:

// File Name: uva297.c// Author: darkdream// Created Time: 2013年05月20日 星期一 11时03分48秒#include
#include
#include
#include
#include
#include
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

 

转载于:https://www.cnblogs.com/zyue/archive/2013/05/21/3090218.html

你可能感兴趣的文章
一款基于Vue的扩展性组件库 VV-UI
查看>>
数组去重
查看>>
Numba(??)
查看>>
JBPM4.4+SSH 整合配置及完整实例
查看>>
java多线程设计模式
查看>>
在Foxmail邮件客户端登录263企业邮箱
查看>>
网站架构不得不谨慎的10个问题
查看>>
SQL查看表数据占用空间代码
查看>>
Linux系统信息查看命令大全
查看>>
jquery ajax 同步异步的执行
查看>>
vue 渲染流程
查看>>
pytorch bug
查看>>
centos7.3使用squid搭建代理服务器
查看>>
温故而知新-WebSocket 教程
查看>>
OO第二次总结 多线程从玄幻到入门
查看>>
EasyUI中, datagrid用loadData方法绑定数据。
查看>>
收藏的比较好的 博客园的文章(不断完善中)
查看>>
【转载】云存储的故事---元数据归来
查看>>
随笔:拼了命找工作
查看>>
vue----webpack模板----vuex----modules子模块
查看>>