297 - Quadtrees (UVa)
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.
题意:这道题的意思是有一块图片,总共分为1024块像素。然后每一个大正方形可以分为四块。以此类推,直到最后分为最小的1024块像素之一。
即每棵树如果有子节点,那么就会有4个子树。具体看一下上面的图就一目了然了。
那么这道题我看到四个儿子就退缩了。没有用树来做。
反正也就1024块方块,我就直接用模拟的形式将其染色。在输入的时候,利用栈的原理来做。就是只有非叶子才能入栈,然后要用变量来标记一下这是第几个儿子。
不过我看了网上其他人的代码,用树来做也不是很麻烦。不过做起来还是没有直接染色做的方便吧。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#define maxn 1024
using namespace std;
struct node
{
char ch;
int x;
}temp;
char ch;
int t,i,j,lz;
int a1[1200]= {0},a2[1200]= {0};
void input(int *a1)
{
stack<node> s;
temp.ch='p';
temp.x=0;
int bj=0;
s.push(temp);
while(cin>>ch)
{
if (ch=='p')
{
temp=s.top();
s.pop();
temp.x++;
s.push(temp);
temp.ch='p';
temp.x=0;
s.push(temp);
}
else
{
temp=s.top();
lz=maxn/pow(4,(s.size()-1));
if (ch=='f')
{
for (i=bj; i<bj+lz; i++)
a1[i]=1;
}
bj=bj+lz;//这个变量很重要,每次分块之后,不过染没染色,要移动一个模块。
s.pop();
temp.x++;
s.push(temp);
}
temp=s.top(); while(temp.x==4)
{
s.pop();
temp=s.top();
}
//cout<<temp.x<<endl;
if (s.size()==1) break;
}
s.pop();
}
int main ()
{
char s1[2000],s2[2000];
cin>>t;
while(t--)
{
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
input(a1);
input(a2);
int sum=0;
for (i=0; i<maxn; i++)
if (a1[i] || a2[i])
sum++;
printf("There are %d black pixels.\n",sum);
}
return 0;
}
297 - Quadtrees (UVa)的更多相关文章
- UVa 297 Quadtrees(树的递归)
Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...
- UVA.297 Quadtrees (四分树 DFS)
UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...
- uva 297 quadtrees——yhx
Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind ...
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: r ...
- UVa 297 Quadtrees -SilverN
A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...
- UVa 297 - Quadtrees
题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色. 利用递归根据字符串, ...
- UVA - 297 Quadtrees (四分树)
题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...
- 【紫书】Quadtrees UVA - 297 四叉树涂色
题意:前序遍历给出两个像素方块.求两个方块叠加后有几个黑色格子. 题解:每次读进来一个方块,就在二维数组上涂色.每次把白色涂黑就cnt++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...
- 四分树 (Quadtrees UVA - 297)
题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...
随机推荐
- 使用Javascript限制文本框只允许输入数字
很多时候需要用到限制文本框的数字输入,试过许多方法,都不太理想,遂决定自己实现一个来玩玩.曾经使用过的方法通过onkeydown事件来控制只允许数字: <input onkeydown=&quo ...
- singleTask TaskAffinity allowTaskReparenting
关于singleTask TaskAffinity allowTaskReparenting 一.Activity的LaunchMode 1.standard 2.singleTop:FLAG_ACT ...
- 加速器eaccelerator不兼容高版本php
话说PHP官方发布PHP5.4已经有一阵了,根据使用的情况来看,似乎还是很不错的.从初始发布到现在升级到的PHP5.4.4,修正不少的Bug.PHP5.4新的版本,除了提供了更多新的特性,还有大幅的效 ...
- AutoLayout(转)
转自 http://blog.sina.com.cn/s/blog_9564cb6e0101wv9o.html controller和View的责任分配: 1.View指定固有的content ...
- 使用less函数实现不同背景的CSS样式
今天在公司遇到一个比较特殊的需求,需要完成这样的布局,如下图: 每一个块的背景需要不同,而其他都是相同的,这时候就应该把背景提出来单独写成一个CSS样式类. 那么问题来了,有四个不同的背景需要写4个基 ...
- Spring AOP原理及拦截器
原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...
- 对于方法 String.Contains,只支持可在客户端上求值的参数。
var ProjectLevel_XJJS = "06,07,08,09"; p.Where(e =>ProjectLevel_XJJS.Contains(e.LevelCo ...
- oracle删除互相关联的记录
今天遇到一个问题,在数据库中删除一条记录,但是在删除的时候报错了,报出该记录已经被其他子记录引用,想了好久不知道怎么做,后来发现报错提示信息中会提示删除该记录时影响了那个约束条件,于是思路出来了: 1 ...
- angularJS中如何写控制器
angularJS中的控制器是一个函数,用来向视图作用域中添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为 当我们在页面上创建一个新的控制器时,angularJS会生成并传递一个新 ...
- checkbox复选框全选批量删除
多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="Form ...