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)的更多相关文章

  1. UVa 297 Quadtrees(树的递归)

    Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...

  2. UVA.297 Quadtrees (四分树 DFS)

    UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...

  3. uva 297 quadtrees——yhx

    Quadtrees  A quadtree is a representation format used to encode images. The fundamental idea behind ...

  4. UVA 297 Quadtrees(四叉树建树、合并与遍历)

    <span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: r ...

  5. UVa 297 Quadtrees -SilverN

    A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...

  6. UVa 297 - Quadtrees

    题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色. 利用递归根据字符串, ...

  7. UVA - 297 Quadtrees (四分树)

    题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...

  8. 【紫书】Quadtrees UVA - 297 四叉树涂色

    题意:前序遍历给出两个像素方块.求两个方块叠加后有几个黑色格子. 题解:每次读进来一个方块,就在二维数组上涂色.每次把白色涂黑就cnt++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...

  9. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

随机推荐

  1. 国内优秀npm镜像

    淘宝npm镜像 淘宝npm镜像:http://npm.taobao.org/ 1.临时使用 npm --registry https://registry.npm.taobao.org install ...

  2. MeasureSpec

    在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...

  3. 关于timestamp的二三事

    之所以要写timestamp的随笔,是因为之前对它的理解存在误区,so. I have to remind myself by writing this informal essay. 微软文档链接: ...

  4. 导入excel错误:外部表不是预期的格式 解决方案(Oledb)

    -----转载:http://blog.csdn.net/zhou349398998/article/details/8740424 环境:win7+iis7+Office2007 在asp.net网 ...

  5. 加速器eaccelerator不兼容高版本php

    话说PHP官方发布PHP5.4已经有一阵了,根据使用的情况来看,似乎还是很不错的.从初始发布到现在升级到的PHP5.4.4,修正不少的Bug.PHP5.4新的版本,除了提供了更多新的特性,还有大幅的效 ...

  6. sqlserver2008附加数据库——错误3415

    权限问题, 在其文件,右击属性>安全>编辑>添加>加一个everyone单击确定>其完全控制, 这样给每个用户权限 ---来自凌波小屋----冯和超笔记-----

  7. C#设置IP地址,启用禁用适配器

    界面效果图如下: 报表界面 说下关键代码 需要开启 Windows Management Instrumentation服务(默认已经开启),在程序中需要增加 Management引用. 主要有Net ...

  8. 加入BOINC(伯克利开放式网络计算平台)

    转载:BOINC:为科学而计算 通过 BOINC 你可以将闲置的计算机时间贡献给 SETI@home, Climateprediction.net, Rosetta@home, World Commu ...

  9. android 05

    控件:RadioButton CheckedBox RatingBar ProgressBar 下拉列表:ListView Spinner <!-- 单选按钮必须放在单选按钮组当中才能生效 ,并 ...

  10. JSON基础知识总结

    JSON基础 一.JSON简介 JSON,全称“JavaScript Object Notation(JavaScript对象表示法)”,起源于JavaScript的对象和数组.JSON,说白了就是J ...