Quadtrees--四叉树

Description

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.
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 const int INF=0x5fffffff;
18 const double EXP=1e-8;
19 const int mod=100000007;
20 const int MS=1500;
21 const int len=32;
22 char str[MS];
23 int buf[len][len];
24 int ans;
25 //2 1
26 //3 4
27
28 void draw(const char *s,int &p,int r,int c,int w)
29 {
30     char ch=s[p++];
31     if(ch==‘p‘)
32     {
33         draw(s,p,r,c+w/2,w/2);  //1
34         draw(s,p,r,c,w/2);      //2
35         draw(s,p,r+w/2,c,w/2);  //3
36         draw(s,p,r+w/2,c+w/2,w/2);//4
37     }
38     else if(ch==‘f‘)  //处理黑色像素
39     {
40         for(int i=r;i<r+w;i++)
41             for(int j=c;j<c+w;j++)
42         {
43             if(buf[i][j]==0)   //两个图像的相同的位置都是黑色像素的话,
44             {                  //是算一个的,buf其标记的作用
45                 buf[i][j]=1;
46                 ans++;
47             }
48         }
49     }
50 }
51
52 int main()
53 {
54     int T;
55     scanf("%d",&T);
56     while(T--)
57     {
58         memset(buf,0,sizeof(buf));
59         ans=0;
60         int p=0;
61         scanf("%s",str);
62         draw(str,p,0,0,len);
63         scanf("%s",str);
64         p=0;
65         draw(str,p,0,0,len);
66         printf("There are %d black pixels.\n",ans);
67     }
68     return 0;
69 }
 
时间: 2024-10-08 19:31:11

Quadtrees--四叉树的相关文章

d3浅谈

d3是一个及其庞大的库,有20个模块,大小也达到了216kb,是JQ1.x的2倍多,JQ3.x的3倍多,JQ本来就挺笨重的一个库,d3更是如此,但是它的功能确实很强悍~ d3的定位是一个科学计算库,并不是一个图形库,只是它能帮你算出路径,但是把路径画成svg图它是没有帮你的,有时候算出来的路径还要经过一系列处理. 所以说人生苦短,莫用d3~~当然,作为一个有追求的程序猿萌新,对这么一个优秀的库,我们还是秉承着了解学习的态度,下面由我带大家一起大概领略一下d3吧~ 官网:https://d3js.

UVA 297 Quadtrees(四叉树建树、合并与遍历)

<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">K - </span><span style="color: blue; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color

[译]2D空间中使用四叉树Quadtree进行碰撞检测优化

操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity2017.2.0f3 原文出处 : Quick Tip: Use Quadtrees to Detect Likely Collisions in 2D Space 许多游戏需要使用碰撞检测算法去判定两个对象是否发生碰撞,但是这些算法通常意味着昂贵操作,拖慢游戏的运行速度.在这篇文章中我们将会学习四叉树 quadtrees,并学习如果通过四叉树跳过那些物理空间距离比较远的对象,最终提高碰撞检测速度. 注:原文

四叉树算法原理与实现

一.原理 四叉树编码的基本思想是:首先将把一副图像或栅格地图( ,k>1,不足则补网)等分成四个一级字块,顺序为左上,右上,左下,右下:然后逐块检查其中所有格网属性值(或灰度值),若相同,则该字块不再分:若不同,则将该子块进一步分成四个二级子块:如此递归地分割,直到每个子块的属性或灰度均相等为止. 二.算法实现 1 //实现四叉树编码 2 3 #include"stdio.h" 4 void Qutree(int arysize,int level,float curary[]

Unity 遍历敌人——使用四叉树空间分区

最近看了<游戏编程模式>这本书,里面有一篇空间分区的文章,看了心里痒痒,决定去尝试实现一下.文章后面会给出整个学习参考的链接. 实现的效果如下,我们有一个很大的场景,场景有许许多多的敌人.红色的点代表是玩家,黑色的点代表是敌人.在这样的一个大量敌人的情景下,我们不可能在玩家或敌人寻找身边的攻击对象时穷尽所有的对象.因为我们要建立空间分区,只遍历某个对应区的对象.在图下中,红点中遍历红框中的黑点对象,其他一律不遍历. 接下来我直接放代码了,主要采用了四叉树,如果对于一些不懂的地方断点调试下就可以

[转]一个四叉树Demo学习

程序代码: http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C 四叉树: using System; using System.Drawing; using System.Collections.Generic; using System.Diagnostics; namespace QuadTreeLib { /// <summary> /// A Quadtree is a stru

HTML5实现3D和2D可视化QuadTree四叉树碰撞检测

QuadTree四叉树顾名思义就是树状的数据结构,其每个节点有四个孩子节点,可将二维平面递归分割子区域.QuadTree常用于空间数据库索引,3D的椎体可见区域裁剪,甚至图片分析处理,我们今天介绍的是QuadTree最常被游戏领域使用到的碰撞检测.采用QuadTree算法将大大减少需要测试碰撞的次数,从而提高游戏刷新性能,本文例子基于HT for Web的Canvas拓扑图和WebGL的3D引擎组件,通过GraphView和Graph3dView共享同一数据模型DataModel,同时呈现Qua

BNUOJ 4049 四叉树

四叉树 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main 四叉树是一种常用的数据结构,广泛应用于栅格数据(如地图数据.遥感图像)的压缩编码中.将四叉树扩展到三维就形成了八叉树,可实现三维信息的压缩存储.下面是它的实现原理: 如图是一个8*8图象,如果该图像所有元素都一样(都是0或都是1),就编码为0,再加上公共一样的元素(如01或00).如果不一样,

回想四叉树LOD地形(上)

唉.~事实上这是在差点儿相同一年前实现的东西,但当时没作好记录.放了那么久了,假设不做点总结的话,好像有点对不起自己,于是·········还是做点什么吧. 我脑洞比較小,仅仅能大量參考"潘李亮"大神的四叉树LOD地形论文和一个叫做"曾涛"大神的代码实现,小弟在此先向诸位致敬了.以下分享本人实践之后的一点心得. 为什么要用LOD地形? 或许看完了传说中的"龙书"有关地形那章,你就知道了一个简单的地形系统是怎么实现的,无非就是依据高度图提供的高度信

HT for Web可视化QuadTree四叉树碰撞检测

QuadTree四叉树顾名思义就是树状的数据结构,其每个节点有四个孩子节点,可将二维平面递归分割子区域.QuadTree常用于空间数据库索引,3D的椎体可见区域裁剪,甚至图片分析处理,我们今天介绍的是QuadTree最常被游戏领域使用到的碰撞检测.采用QuadTree算法将大大减少需要测试碰撞的次数,从而提高游戏刷新性能,本文例子基于HT for Web的图形引擎,通过GraphView和Graph3dView共享同一数据模型DataModel,同时呈现QuadTree算法下的2D和3D碰撞视图