题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积。

分析:搜索、数据结构。把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色。

利用递归根据字符串,建立相应四叉树。在建树的过程中,树节点计算当前节点对应的小正方形

编号区间。这里处理类似于线段树,将父节点的区间等分成4份分别对应四棵子树的编号区间。

建树到达叶子时(color为‘f’或者‘e’),直接将颜色数组赋值即可。当树建完时,颜色数组即染色

完毕。将两棵树依次染色到同一数组,统计黑色节点个数即可。

注意:数组大小,防止RE。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath> using namespace std; int Color[1024]; typedef struct tnode
{
char color;
tnode* child[4];
}tnode;
tnode *root;
tnode Node[1365];
int Count; tnode* dfs( char* word, int &move, int a, int b )
{
tnode* root = &Node[Count ++];
root->color = word[move ++];
if ( root->color == 'p' ) {
int mid = (a+b)>>1;
int m_l = (a+mid)>>1;
int m_r = (mid+b)>>1;
//构建四个子树
root->child[0] = dfs( word, move, a, m_l );
root->child[1] = dfs( word, move, m_l+1, mid );
root->child[2] = dfs( word, move, mid+1, m_r );
root->child[3] = dfs( word, move, m_r+1, b );
}else if ( root->color == 'f' ) {
//叶子节点染色
for ( int i = a ; i <= b ; ++ i )
Color[i] = 1;
}return root;
} int main()
{
int n,move;
char data[1366]; while ( ~scanf("%d",&n) )
for ( int i = 0 ; i < n ; ++ i ) {
for ( int i = 0 ; i < 1024 ; ++ i )
Color[i] = 0; //处理图片1
scanf("%s",data);
Count = 0;
dfs( data, move = 0, 0, 1023 ); //处理图片2
scanf("%s",data);
Count = 0;
dfs( data, move = 0, 0, 1023 ); //统计黑色节点
int sum = 0;
for ( int i = 0 ; i < 1024 ; ++ i )
sum += Color[i];
printf("There are %d black pixels.\n",sum);
} return 0;
}

UVa 297 - Quadtrees的更多相关文章

  1. UVa 297 Quadtrees(树的递归)

    Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...

  2. UVA.297 Quadtrees (四分树 DFS)

    UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...

  3. uva 297 quadtrees——yhx

    Quadtrees  A quadtree is a representation format used to encode images. The fundamental idea behind ...

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

    <span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: r ...

  5. UVa 297 Quadtrees -SilverN

    A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...

  6. UVA - 297 Quadtrees (四分树)

    题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...

  7. 297 - Quadtrees (UVa)

    Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind t ...

  8. UVa 297 (四分树 递归) Quadtrees

    题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...

  9. 【紫书】Quadtrees UVA - 297 四叉树涂色

    题意:前序遍历给出两个像素方块.求两个方块叠加后有几个黑色格子. 题解:每次读进来一个方块,就在二维数组上涂色.每次把白色涂黑就cnt++: 具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往 ...

随机推荐

  1. PHP设计模式之装饰者模式

    <?php /* 装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. */ header("Content-type:text/html; cha ...

  2. ORACLE解锁record is locked by another user

    在操作ORACLE数据库的时候,由于执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user ...

  3. activiti 源码笔记之startProcess

    rumtimeService.startProcessInstanceByXX方法将启动流程的任务委派给StartProcessInstanceCmd,此时会根据rumtimeService.star ...

  4. Python内置数据类型之List篇

    List的定义: li = ["one" , "two" , "three" , "four"] List是一个有序的集 ...

  5. Python网页解析

    续上篇文章,网页抓取到手之后就是解析网页了. 在Python中解析网页的库不少,我最开始使用的是BeautifulSoup,貌似这个也是Python中最知名的HTML解析库.它主要的特点就是容错性很好 ...

  6. ProgressBar及其子类

    1.ProgressBar(进度条组件) 派生了两个常用的组件:SeekBar和RatingBar. <1>通过style属性可以为ProgressBar指定风格,该属性可支持如下几个属性 ...

  7. python tile函数用法

    tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...

  8. IOS 单例 创建方式

    @implementation Me static Car *sharedInstance= nil;//声明一个静态对象引用并赋为nil +(Me *) sharedInstance//声明类方法( ...

  9. HUD 2717 Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  10. Codeforces Round #215 (Div. 2) D题(离散化+hash)

    D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...