树--四分树(UVa297)
郑重声明: 数据结构这部分内容, 由于博主才学很少(且很浅)的内容, 所以现在所写的(大都是抄的)一些典型例题, 再加上一些自己想法和理解而已, 等博主勤加修炼, 以后会大有补充和改进。 粗浅之处, 还望大牛们哈哈一笑!
UVa297 - Quadtrees
Time limit: 3.000 seconds
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.
#include<cstdio>
#include<cstring> const int len = ;
const int maxn = + ;
char s[maxn];
int buf[len][len], cnt; //把字符串s[p..]导出到以(r,c)为左上角, 边长为 w 的缓冲区中。
//2 1
//3 4
void draw(const char* s, int& p, int r, int c, int w)
{
char ch = s[p++];
if(ch == 'p')//看着眼熟吗? 对,就是棋盘覆盖, 又是递归。
{
draw(s, p, r, r+w/, w/);
draw(s, p, r, c , w/);
draw(s, p, r+w/, c , w/);
draw(s, p, r+w/, c+w/, w/);
} else if(ch == 'f')
{//画黑像素(白像素不画)
for(int i=r; i<r+w; i++)
for(int j=c; j<c+w; j++)
if(buf[i][j] == )
{
buf[i][j] = ; cnt++;
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(buf, , sizeof(buf));
cnt = ;
for(int i=; i<; i++)
{
scanf("%s", s);
int p = ;
draw(s, p, , , len);
}
printf("There are %d black pixels.\n", cnt);
}
return ;
}
树--四分树(UVa297)的更多相关文章
- Uva297 Quadtrees【递归建四分树】【例题6-11】
白书 例题6-11 用四分树来表示一个黑白图像:最大的图为根,然后按照图中的方式编号,从左到右对应4个子结点.如果某子结点对应的区域全黑或者全白,则直接用一个黑结点或者白结点表示:如果既有黑又有白,则 ...
- UVA806-Spatial Structures(四分树)
Problem UVA806-Spatial Structures Accept:329 Submit:2778 Time Limit: 3000 mSec Problem Description ...
- 四分树 (Quadtrees UVA - 297)
题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...
- UVa 297 (四分树 递归) Quadtrees
题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...
- UVA - 297 Quadtrees (四分树)
题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...
- 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表
4513: [Sdoi2016]储能表 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 395 Solved: 213[Submit][Status] ...
- [C++]四分树(Quadtrees)
[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] 四分树Quadtrees 一幅图有1024个点, 可以对图平均分成4块, 并且子图也可以再往下分, 直到一个 ...
- UVA.297 Quadtrees (四分树 DFS)
UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...
- UVa 806 四分树
题意: 分析: 类似UVa 297, 模拟四分树四分的过程, 就是记录一个左上角, 记录宽度wideth, 然后每次w/2这样递归下去. 注意全黑是输出0, 不是输出1234. #include &l ...
随机推荐
- Datatables带参重绘
研究了好久,最后发现只需要加上参数("bDestory":true,) 即可实现每次刷新就是新的重绘,而无需调用什么desctory init clear等等函数..
- linux下配置环境变量【原创】
用户目录下的.profile, 你只需要在用户目录下,创建bin目录,在里面放入你想要执行的可执行文件,就可以在命令行下就可以使用了 # ~/.profile: executed by the com ...
- WIN7(VISTA)系统无法上网问题排查方法
WIN7(VISTA)系统无法上网问题排查方法 一.无法通过DHCP自动获取到IP 1. 确认正确配置路由器的DHCP功能 a.一般租期建议设置为1-3小时,推荐设置1小时. b.DHCP地址池不要和 ...
- umask:遮罩码
查看umask:umask 创建文件:-umask 文件默认不能具有执行权限 创建目录:-umask 设置umask:umask 0022 生效访范围:当前shell
- 使用phpmyadmin导入SQL数据报错:#1062 - Duplicate entry '...
使用phpmyadmin导入SQL数据报错: MySQL 返回: #1062 - Duplicate entry '0' for key 'PRIMARY' 出现此错误的原因是数据的主键重复了,错误信 ...
- html+css复习之第2篇 | javascript
1. java 中定义数组和对象: 数组(Array)字面量 定义一个数组: [40, 100, 1, 5, 25, 10] 对象(Object)字面量 定义一个对象: {firstName:&quo ...
- hashcode与equals
归纳一下就是hashCode是用于查找使用的,而equals是用于比较两个对象的是否相等的.以下这段话是从别人帖子回复拷贝过来的: .hashcode是用来查找的,如果你学过数据结构就应该知道,在查找 ...
- Java获取当前第几周【转】
本文copy自:http://swxzqsd.blog.sohu.com/156208509.html 作者:camelcanoe String today = "2010-01-11&qu ...
- [Ogre][地形]OgreTerrain分析以及使用
Ogre 1.7.2中的地形教程 ○读者可以对照着Ogre1.7.2中的terrain.h源码进行阅读加深理解,蓝色部分均为源码 ○去除了一些具体场景比如添加mesh,设置setAmbientLigh ...
- JavaScript DOM 编程艺术(第2版)读书笔记 (8)
<!--这章的内容略奇怪啊!可能是因为我之前没有接触过这些知识点,等以后用到的时候再回来翻阅吧,现在先简要介绍一下 js权限已通过,博客园好快的效率啊,谢谢O(∩_∩)O --> 缩略语 ...