uva 297 quadtrees——yhx
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.
#include<cstdio>
#include<cstring>
int map[][];
void draw(int x,int y,int l)
{
int i,j,k,p,q;
char c;
scanf("%c",&c);
if (c=='p')
{
draw(x,y+l/,l/); //四分
draw(x,y,l/);
draw(x+l/,y,l/);
draw(x+l/,y+l/,l/);
}
if (c=='f')
for (i=x;i<=x+l-;i++) //注意减1
for (j=y;j<=y+l-;j++)
map[i][j]=;
}
int main()
{
int i,j,k,n,ans;
char c;
scanf("%d",&n);
for (i=;i<=n;i++)
{
scanf("%*c");
memset(map,,sizeof(map)); //清空
draw(,,);
scanf("%*c"); //读入回车符
draw(,,);
ans=;
for (j=;j<=;j++)
for (k=;k<=;k++)
if (map[j][k]) ans++;
printf("There are %d black pixels.\n",ans);
}
}
其实和前两道题差不多,都是递归求树。
uva 297 quadtrees——yhx的更多相关文章
- UVa 297 Quadtrees(树的递归)
Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...
- UVA.297 Quadtrees (四分树 DFS)
UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...
- 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& ...
- 297 - Quadtrees (UVa)
Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind t ...
- UVa 297 (四分树 递归) Quadtrees
题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...
- 【紫书】Quadtrees UVA - 297 四叉树涂色
题意:前序遍历给出两个像素方块.求两个方块叠加后有几个黑色格子. 题解:每次读进来一个方块,就在二维数组上涂色.每次把白色涂黑就cnt++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...
随机推荐
- 【poj 3461】Oulipo(字符串--KMP)
题意:求子串在文本串中出现了多少次. 解法:使用KMP的next[ ]和tend[ ]数组计数. #include<cstdio> #include<cstdlib> #inc ...
- Hibernate中的一级缓存、二级缓存和懒加载(转)
1.为什么使用缓存 hibernate使用缓存减少对数据库的访问次数,从而提升hibernate的执行效率.hibernate中有两种类型的缓存:一级缓存和二级缓存. 2.一级缓存 Hibenate中 ...
- 最熟悉的陌生人-------MVC
以前开发iOS程序的时候用的最多的是MVC的设计模式,这种软件架构的模式是由:模型(Model)[屏幕中展示的].视图(View)[如何展示的]和控制器(Controller)[程序的数据 ...
- android开发布局文件imageview 图片等比例缩放:
ImageView的属性scaleType,如果等比缩放的话,就使用CenterInside,如果想固定大小的话,就CenterCrop <?xml version="1.0" ...
- [Tool] 使用Visual Studio Code开发TypeScript
[Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在「Windows」.「OS X」操作系统上,使用Visual Studio Code ...
- apple store链接格式文档
备份一下: The app on Appstore has specific URL format http://itunes.apple.com/[country-code]/app/[app-na ...
- XML的约束(schema)
XML Schema也是一种用于定义和描述XML文档结构与内容的模式语言,其出现是为了克服DTD的局限性 XML Schema符合XML语法结构 DOM.SAX等XML API很容易解析出XML Sc ...
- oracle10g 统计信息查看、收集
1. 统计信息查看 1.1 单个表的全局统计信息.统计效果查看 2. 统计信息分析(收集) 2.1 分析工具选择 2.2 分析前做index重建 2.3 分析某数据表,可以在PL/SQL的comm ...
- Office 365 - SharePoint Tips & Tricks
1. Recycle Bin 地址: //管理员 /_layouts/15/AdminRecycleBin.aspx //普通用户 /_layouts/15/RecycleBin.aspx 2.
- 解析XML的几种方法之SAX解析
假期总结不能停,坚持坚持....接下来总结一下XMl和json的解析和生成.. 解析XML的四种方法,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址: DOM:在现 ...