UVA 297 Quadtrees(四叉树建树、合并与遍历)
<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">K - </span><span style="color: blue; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Quadtrees</span>
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
System Crawler (2014-01-02)
Description

Quadtrees |
A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants,
etc. In the quadtree, the image is represented by a parent node, while the four quadrants are represented by four child nodes, in a predetermined order.
Of course, if the whole image is a single color, it can be represented by a quadtree consisting of a single node. In general, a quadrant needs only to be subdivided if it consists of pixels of different colors.
As a result, the quadtree need not be of uniform depth.
A modern computer artist works with black-and-white images of units, for a total
of 1024 pixels per image. One of the operations he performs is adding two images together, to form a new image. In the resulting image a pixel is black if it was black in at least one of the component images, otherwise it is white.
This particular artist believes in what he calls the preferred fullness: for an image to be interesting (i.e. to sell for big bucks) the most important property is the number of filled (black) pixels
in the image. So, before adding two images together, he would like to know how many pixels will be black in the resulting image. Your job is to write a program that, given the quadtree representation of two images, calculates the number of pixels that are
black in the image, which is the result of adding the two images together.
In the figure, the first example is shown (from top to bottom) as image, quadtree, pre-order string (defined below) and number of pixels. The quadrant numbering is shown at the top of the figure.
Input Specification
The first line of input specifies the number of test cases (N) your program has to process.
The input for each test case is two strings, each string on its own line. The string is the pre-order representation of a quadtree, in which the letter 'p' indicates a parent node, the letter 'f'
(full) a black quadrant and the letter 'e' (empty) a white quadrant. It is guaranteed that each string represents a valid quadtree, while the depth of the tree is not more than 5 (because each pixel has only one color).
Output Specification
For each test case, print on one line the text 'There are X black pixels.', where X is the number of black pixels in the resulting image.
Example Input
3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe
Example Output
There are 640 black pixels.
There are 512 black pixels.
There are 384 black pixels.
题意:有一个用四叉树表示的图,该图用P,E,F来表示,P表示父节点,F表示黑色,E表示白色,整个图的大小为1024。每个子图都能分成四个部分(当颜色不同的时候才须要划分),如今要把两个图合并成一个图,求合并后图有多少黑色像素。
#include<stdio.h>
#include<cstring>
#include<algorithm>
int T;
char s1[2049],s2[2049];
struct quadtree
{
int num;
quadtree *next[4];
quadtree()
{
num=0;
for(int i=0; i<4; i++)next[i]=0;
}
};
quadtree *build(char *s)///建树
{
quadtree *now=new quadtree;
int len=strlen(s);
if(s[0]!='p')
{
now->num=1;
if(s[0]!='f')
{
delete now;
now=NULL;
}
return now;
}
int up=4;///子树数目
int d=1;
for(int i=1; d<=up&&i<len; i++)
{
if(s[i]=='p')
{
now->next[d-1]=build(s+i);
int dx=0,dy=4;
while(dx<dy)
{
dx++;
if(s[i+dx]=='p')dy+=4;
}
i+=dx;///i变到下一颗子树的起始位置
}
else
{
now->next[d-1]=build(s+i);
}
d++;
}
return now;
}
quadtree *merge_(quadtree *p,quadtree *q)///合并
{
if(p||q)
{
quadtree *root=new quadtree;
if(p&&q)for(int i=0; i<4; i++)
{
if(p->num||q->num)
{
root->num=1; ///子树已经全为黑色,不须要继续递归
continue;
}
root->next[i]=merge_(p->next[i],q->next[i]);
}
else if(p==NULL&&q)for(int i=0; i<4; i++)
{
if(q->num)
{
root->num=1;; ///子树已经全为黑色,不须要继续递归
continue;
}
root->next[i]=merge_(NULL,q->next[i]);
}
else for(int i=0; i<4; i++)
{
if(p->num)
{
root->num=1;; ///子树已经全为黑色,不须要继续递归
continue;
}
root->next[i]=merge_(p->next[i],NULL);
}
return root;
}
return NULL;
}
int dfs(quadtree *p,int num)
{
if(p==NULL)return 0;
int sum=0;
if(p->num)sum+=num;
for(int i=0; i<4; i++)
{
sum+=dfs(p->next[i],num/4);
}
return sum;
}
int main()
{
//freopen("in.txt","r",stdin);
quadtree *root1,*root2,*root;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",s1,s2);
root=root1=root2=NULL;
root1=build(s1);
root2=build(s2);
root=merge_(root1,root2);
printf("There are %d black pixels.\n",dfs(root,1024));
}
return 0;
}
UVA 297 Quadtrees(四叉树建树、合并与遍历)的更多相关文章
- UVA.297 Quadtrees (四分树 DFS)
UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...
- UVa 297 Quadtrees(树的递归)
Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...
- uva 11234 Expressions 表达式 建树+BFS层次遍历
题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...
- UVA - 297 Quadtrees (四分树)
题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...
- UVa 297 - Quadtrees
题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色. 利用递归根据字符串, ...
- uva 297 quadtrees——yhx
Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind ...
- UVa 297 Quadtrees -SilverN
A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...
- 【紫书】Quadtrees UVA - 297 四叉树涂色
题意:前序遍历给出两个像素方块.求两个方块叠加后有几个黑色格子. 题解:每次读进来一个方块,就在二维数组上涂色.每次把白色涂黑就cnt++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...
- Quadtrees UVA - 297
题目链接:https://vjudge.net/problem/UVA-297 题目大意:如上图所示,可以用一个四分树来表示一个黑白图像,方法是用根节点表示整副图像,然后把行列各等分两等分,按照图中的 ...
随机推荐
- STM32下载方法
一.JLINK下载方法 1.硬件设置 Boot0,Boot1 = 0,*(测试通过) Boot0,Boot1 = 1,0或者0,1(未测试) 2.软件设置 MDK设置 ① 选择Project -> ...
- VS2010升级VS2012必备(MVC4 WebPage2.0 Razor2.0资料汇集)
刚把项目升级到2012,发现发生了很多变化,以下是最近看过的网站和资料汇集,供需要者参考. 本文在最近一个月可能会不断更新. Razor2.0 新特性介绍: 介绍1:http://vibrantcod ...
- WiFi无线模块学习1——HLK-M30使用
产品概述 概述: 通过该模块,传统的串口设备在不需要更改任何配置的情况下,即可通过Internet 网络传输自己的数据.为用户的串口设备提供完整快读的解决方案. 技术参数 可查询技术规格表 主要应用领 ...
- Solve Longest Path Problem in linear time
We know that the longest path problem for general case belongs to the NP-hard category, so there is ...
- hdu 2767
这也是道强连通分量的题: 题目要求我们求出最少需要添加多少条边让整个图变成一个强连通分量: 思路很简单,直接缩点,然后找出所有点中有多少出度为0,入度为0的点,最大的那个就是题目所求: 贴代码: #i ...
- Django自定义上传目录
由于数据库的upload_to功能,有时不能满足每次上传灵活自定义的需求, 基于DEF的上传,有时不能满足基于CLASS的视图要求, 于是,只好慢慢用土法实现. 当然,首先,要使用上传功能时,form ...
- CRC算法及C实现
一.CRC算法原理 CRC校验的基本思想是利用线性编码理论,在发送端根据要传送的k位二进制码序列,以一定的规则产生一个校 验用的监督码(既CRC码)r位,并附在信息后边,构成一个新的二进制码序列数 ...
- 基于开源软件在Azure平台建立大规模系统的最佳实践
作者 王枫 发布于2014年5月28日 前言 Microsoft Azure 是微软公有云的唯一解决方案.借助这一平台,用户可以以多种方式部署和发布自己的应用. 这是一个开放的平台,除了对于Windo ...
- [转]Unity 3D旋转矢量方向及二维平面基于一点选择另一点(Rotate a Vector3 direction & Rotate a point about another point in 2D )
http://specialwolf.blog.163.com/blog/static/124466832201301332432766/ ****************************** ...
- 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 二)
原文网址:http://www.cnblogs.com/ChinaKingKong/p/4964549.html 前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及 ...