UVa 297 Quadtrees -SilverN
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<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int T;
int mp[][];
char s[];
int ans=;
void draw(char s[],int &p,int r,int c,int w){//在mp上的一个正方形区域中操作,其中(r,c)为区域左上角坐标,w为区域边长
char ch=s[p++];//p为目前处理到的位数,用&实现传值
if(ch=='p')//处理中间节点
{
draw(s,p,r,c+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,j;i<r+w;i++)
for(j=c;j<c+w;j++)
if(mp[i][j]==){
mp[i][j]=;
ans++;
}
}
return;
}
int main(){
scanf("%d",&T);
while(T--){
memset(mp,,sizeof(mp));
ans=;
for(int i=;i<=;i++){//先建第一棵树,再把第二棵树覆盖上去
scanf("%s",s);
int p=;
draw(s,p,,,); }
printf("There are %d black pixels.\n",ans);
}
return ;
}
UVa 297 Quadtrees -SilverN的更多相关文章
- 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
题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成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++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...
随机推荐
- 20款免费响应式的 HTML5 网站模板下载
今天这篇文章给大家带来的是20款免费响应式的 HTML5 网站模板,大家可以借助这些优秀的网站模板创建自己的优秀网站.响应式(Responsive Design)设计的目标是要让产品界面能够响应用户的 ...
- 轻松掌握:JavaScript状态模式
状态模式 状态模式(State)允许一个对象在其内部状态改变的时候改变它的行为,对象看起来似乎修改了它的类. 状态模式的使用场景也特别明确,有如下两点: 一个对象的行为取决于它的状态,并且它必须在运行 ...
- android ndk开发(二)实现一个官方demo
实现了一个官方的demo:bitmap-plasma(水波纹) 源代码就在samples文件夹下,可以自己去找. 界面: 建立项目的步骤和配置环境不明白的可以去看:http://www.cnblogs ...
- 干货之UICollectionViewFlowLayout自定义排序和拖拽手势
使用UICollectionView,需要使用UICollectionViewLayout控制UICollectionViewCell布局,虽然UICollectionViewLayout提供了高度自 ...
- 【转】超实用的JavaScript技巧及最佳实践
众所周知,JavaScript是一门非常流行的编程语言,开发者用它不仅可以开发出炫丽的Web程序,还可以用它来开发一些移动应用程序(如PhoneGap或Appcelerator),它还有一些服务端实现 ...
- jQuery Grid With ASP.Net MVC
jQuery Grid 能够在 ASP.NET MVC 中轻松地实现分页. 排序. 筛选以及 jQuery 插件网格中的 CRUD 操作. 具有以下特征: 时尚的表格数据呈现控件. JavaScrip ...
- Git详解之三 Git分支
相关文档 — 更多 Git 基础培训.ppt GIT 使用经验.ppt GIT 介绍.pptx GIT 分支管理是一门艺术.docx Eclipse上GIT插件EGIT使用手册.docx git/gi ...
- WordCount的程序设计没写出来怎么办
这一星期要完成三个小作业,完成前两个已经让我很吃力的了,现在这个WordCount的编程我都没有头绪,不知道从何下手.虽然要求很看起来很简单,可是不知道怎么去设计这个程序,这两天我也在积极找书学习相关 ...
- Apache安装
记录安装Apache的流程,没有进行详细配置,只是记录搭建服务器的流程用于学习Ajax等知识,方便以后重新安装,不用每次都翻别人博客学习安装了,大神看到这里可以关掉这个粗糙简陋的博文了. 1. 官网上 ...
- [转载]DBA的特质第二部分:性格
本文转自http://www.searchdatabase.com.cn/showcontent_84382.htm 支持原创.尊重原创,分享知识! 在本系列文章中,笔者将谈一谈数据库管理员(DBA) ...