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 题目大意:如上图所示,可以用一个四分树来表示一个黑白图像,方法是用根节点表示整副图像,然后把行列各等分两等分,按照图中的 ...
随机推荐
- jquery实现抽奖
用jquery实现抽奖小程序 用jquery实现抽奖小程序 这些日子,到处都可以看到关于微信小程序的新闻或报到,在博客园中写关于微信小程序的也不少.但是今天我要说的不是微信小程序,而是用简单的jq ...
- JavaScript学习代码整理(一)
/** * Created by wyl on 15-1-23. */ function displayDate() { document.getElementById("demo" ...
- Buying Feed, 2010 Nov (单调队列优化DP)
约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里,这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算买 ...
- UML 中关系详解以及在visio中的表示
http://www.cnblogs.com/kittywei/archive/2013/05/15/3079536.html
- C51 的编程规范
编程首要是要考虑程序的可行性,然后是可读性.可移植性.健壮性以及可测试性.这是总则.但是很多人忽略了可读性.可移植性和健壮性(可调试的方法可能歌不相同),这是不对的. 1.当项目比较大时,最好分模块编 ...
- Android MediaPlayer播放一般音频与SoundPool播放短促的音效
[1]使用MediaPlayer实现一般的音频播放 MediaPlayer播放通常的音频文件 MediaPlayer mediaPlayer = new MediaPlayer(); if (medi ...
- Wide character in print at a2.pl line 返回json 需要encode_utf8
centos6.5:/root/test#cat a2.pl use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Heade ...
- [转]使用Xcode和Instruments调试解决iOS内存泄露
虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在.所以了解原理很重要. 这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露, ...
- JavaScript向表格中添加按钮和文本输入框
例子: <?php ?> <html> <head> <meta http-equiv="Content-Type" content=&q ...
- (转载)js----对象直接量
(转载)http://blog.csdn.net/greymouseyu/article/details/4015676 对象直接量提供了另一种创建新对象的方式.对象直接量允许将对象描述文字嵌入到Ja ...