UVA.297 Quadtrees (四分树 DFS)

题意分析

将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块。字符表示规则是:

p表示这个像素块继续分解,e表示当前方格没有像素,即为空,f表示当前像素块为满,黑色。

最后求解两个数合并后的像素块的数量是多少。

最大的像素块数量是1024个。

采用数组模拟,根据所给的字符串,递归建树。字符数组的建四分树的技巧是(k << 2) + i i∈[-2,1]。 这样就可以充分利用数组的空间。

两树合并的技巧,对于某一节点,如果有一个节点的值为0,另一个对应节点的值为非0,那么这个节点合并后的值就为非0值。 或者是当前节点的像素块为此深度所能到达的最大值,那么合并后即为这个深度的最大值。否则的话,继续向下递归合并。

最后输出合并后的结果即可。

代码总览

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#define nmax 1200
#define standerd 1024
#define hh standerd / (1<<((depth-1)*2))
#define INF 0x3f3f3f3f
using namespace std;
int ta[nmax<<2],tb[nmax<<2];
char str[10000];
int pos = 0;
void pushup(int rt, int t[])
{
int sum = 0;
for(int i = -2; i<=1; ++i)
sum+=t[(rt<<2)+i];
t[rt] = sum;
}
void dfs(int t[],int rt,int depth)
{
if(str[pos] == 'e') {
return;
}
if(str[pos] == 'f') {
t[rt] = hh;
}
if(str[pos] == 'p'){
for(int i = -2; i<=1; ++i){
pos++;
dfs(t,(rt<<2)+i,depth+1);
pushup(rt,t);
}
}
}
bool judge(int depth, int rt)
{
if(ta[rt] == hh || tb[rt] == hh) return true;
else return false;
}
int cal(int t1[], int t2[],int rt,int depth, int sum)
{
if((!t1[rt] || !t2[rt])|| judge(depth,rt))
sum = max(t1[rt],t2[rt]);
else
for(int i = -2; i<=1; ++i)
sum += cal(t1,t2,(rt<<2)+i,depth+1,0);
return sum;
} int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--){
memset(ta,0,sizeof(ta));
memset(tb,0,sizeof(tb));
pos = 0;
scanf("%s",str);
dfs(ta,1,1);
pos = 0;
scanf("%s",str);
dfs(tb,1,1);
int sum = cal(ta,tb,1,1,0);
printf("There are %d black pixels.\n",sum);
}
return 0;
}

UVA.297 Quadtrees (四分树 DFS)的更多相关文章

  1. UVa 297 Quadtrees(树的递归)

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

  2. UVA - 297 Quadtrees (四分树)

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

  3. uva 297 quadtrees——yhx

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

  4. UVa 297 - Quadtrees

    题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色. 利用递归根据字符串, ...

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

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

  6. UVa 297 Quadtrees -SilverN

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

  7. Quadtrees(四分树)

    uva 297 Quadtrees Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Subm ...

  8. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

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

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

随机推荐

  1. 即刻开始使用Kotlin开发Android的12个原因(KAD 30)

    作者:Antonio Leiva 时间:Jul, 11, 2017 原文链接:https://antonioleiva.com/reasons-kotlin-android/ 这组文章已到最后了,它们 ...

  2. Soliworks 2016建模细节总结(1)

    Soliworks 2016建模小细节总结(1) 1.Solidworks 2016三维建模的时候,如果想要在一个视图里面呈现出四个视图(包括三个独立的视图以及三维结构的实体模型图),可以直接按鼠标会 ...

  3. Java学习 · 初识 IO流

    IO流   1. 原理与概念 a)     流 i.           流动,流向 ii.           从一端移动到另一端 源头到目的地 iii.           抽象.动态概念,是一连 ...

  4. vista x64 vs2010 win32添加资源 未能完成操作解决办法

    非常痛苦的感觉,不能用vc6,msdn library也不好用,去2k3系统试了下,没有任何问题,无奈想重装系统了,但是太浪费时间,装了虚拟机也是vistax64的,安装之后正常... 卸载重新安装依 ...

  5. scipy 图像处理-深度学习

    scipy 图像处理(scipy.misc.scipy.ndimage).matplotlib 图像处理 from scipy.misc import imread / imsave / imshow ...

  6. 57[LeetCode] Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. 春招实习汇总(7个offer)

    转载出处 刚从北京到家,总算也可以歇歇了,最近一段时间真是忙于奔命的感觉,也确实体会到了找工作的艰辛,总而言之,求职之路,如人饮水,冷暖自知. 我想把这段时间找工作的体验和经历分享出来告诉大家,让大避 ...

  8. POJ 2540 Hotter Colder(半平面交)

    Description The children's game Hotter Colder is played as follows. Player A leaves the room while p ...

  9. 软工2017第四周作业结对编程——个人psp

    29.22 --9.26本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 预计时间 开始时间                结束时间 中断时间 ...

  10. Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...