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++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...
随机推荐
- 【CSS3】 理解CSS3 transform中的Matrix(矩阵)
理解CSS3 transform中的Matrix(矩阵) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu ...
- SQL增强之Merge
SQL Server 2008提供了一个增强的SQL命令Merge,用法参看MSDN:http://msdn.microsoft.com/zh-cn/library/bb510625.aspx 功能: ...
- HTML 表单
HTML 表单包含表单元素. <form> 元素定义 HTML 表单 表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. HTML 表单用于搜集不同类型的用户输入 ...
- ae显示标注
//添加标注,比TextElment功能更强大 public static void ToAddAnnotate(ILayer layer, string fieldName) { IGeoFeatu ...
- linux下的服务器搭建集成环境
linux下的服务器搭建集成环境 ——写给初学者的我们 1.准备工具 1.1 SecureCRT SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录 ...
- Spring Session
开工开工, 准备条件: 1. 本地Redis,官网:http://redis.io/,windows下 https://github.com/ServiceStack/redis-windows ht ...
- 获取设备的mac地址可靠的方法
参考自:http://www.open-open.com/lib/view/open1433406847322.html /** * 获取设备的mac地址 * * @param ac * @param ...
- 属性观察器willSet与didSet
willSet与didSet是swift特有的,就是监听属性值的变化,但有一个小注意点. willSet与didSet调用时机:对象创建后的第一次赋值时开始调用.也就是说,初始化时(包含重载的init ...
- 在iOS开发过程中你遇到这个问题了么?
1.问题:加载UIWebView底部有黑色边框问题. 设置UIWebView opaque为NO,然后设置其背景色为clearColor. 2.问题:iPhone真机输出[UIScreen mainS ...
- iOS开发--Swift 如何完成工程中Swift和OC的混编桥接(Cocoapods同样适用)
由于SDK现在大部分都是OC版本, 所以假如你是一名主要以Swift语言进行开发的开发者, 就要面临如何让OC和Swift兼容在一个工程中, 如果你没有进行过这样的操作, 会感觉异常的茫然, 不用担心 ...