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);
}
}

其实和前两道题差不多,都是递归求树。

最新文章

  1. swift_枚举 | 可为空类型 | 枚举关联值 | 枚举递归 | 树的概念
  2. java 删除目录、 文件
  3. ural 1245. Pictures
  4. 二. log4j配置文件
  5. 结论: blocking_query 是当前堵塞其他会话正在运行的SQL.而不是原始堵塞SQL
  6. iOS中的UIWindow
  7. PHP 9: 表达式
  8. Javascript模块化编程之Why
  9. java变量和数据类型总结
  10. 列表视图(ListView)和ListActivity
  11. Notes : &lt;Hands-on ML with Sklearn &amp; TF&gt; Chapter 7
  12. Fiddler教程--简介
  13. vue render里面的nativeOn
  14. k8s学习笔记之一:kubernetes简介
  15. [Python]可变类型,默认参数与学弟的困惑
  16. 读书笔记:Sheldon.M.Ross:概率论基础教程:2014.01.22
  17. ORM Nhibernet 框架的 CRUD 操作
  18. scala下实现actor多线程基础
  19. MVC4中control的增删改查
  20. CodeM资格赛3

热门文章

  1. C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
  2. sql多行转一行,以逗号隔开
  3. lavarel框架中如何使用ajax提交表单
  4. 在Android设备上判断设备是否支持摄像头
  5. Yii路径总结
  6. linux怎么模糊查找一个文件
  7. GridView总结一:GridView自带分页及与DropDownList结合使用
  8. win-tc图形库编程
  9. 求当前时间100天后的时间日期,格式化为xxxx年xx月xx日
  10. Android源码分析之HandlerThread