/*
解题要有恒心。。。。
不错的一道题,当时没有解出。*****
这道题主要步骤:
*创建一颗四叉树
*合并
*历编
不可盲目去寻找错误,选一测试案例,自己认真走一遍试试
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
using namespace std;
struct Node
{
char ch;
Node *fir,*sec,*thi,*fou;
};
string a;
int sum;
Node *creat(int &loc)
{
/*
创建树的步骤:
步骤1:分配内存
步骤2:递归调用
*/
Node *r=(Node *)malloc(sizeof(Node));
if(loc<a.size())
{
r->ch=a[loc];
if(a[loc]=='p')
{
r->fir=creat(++loc);
r->sec=creat(++loc);
r->thi=creat(++loc);
r->fou=creat(++loc);
}
else
r->fir=r->sec=r->thi=r->fou=NULL;
}
return r;
}
Node *combine(Node *root1,Node *root2)
{
Node *r=(Node *)malloc(sizeof(Node));
if(root1->ch=='f' || root2->ch=='f')
{
r->ch='f';
r->fir=r->sec=r->thi=r->fou=NULL;
}
else if(root1->ch=='e' && root2->ch=='e')
{
r->ch='e';
r->fir=r->sec=r->thi=r->fou=NULL;
}
else
{
r->ch='p';
if(root1->ch=='p' && root2->ch=='p')
{
r->fir=combine(root1->fir,root2->fir);
r->sec=combine(root1->sec,root2->sec);
r->thi=combine(root1->thi,root2->thi);
r->fou=combine(root1->fou,root2->fou);
}
else if(root1->ch=='p' && root2->ch=='e')
{
r->fir=root1->fir;
r->sec=root1->sec;
r->thi=root1->thi;
r->fou=root1->fou;
}
else
{
r->fir=root2->fir;
r->sec=root2->sec;
r->thi=root2->thi;
r->fou=root2->fou;
}
}
return r;
}
void cacluate(Node *root,int len)
{
/*
if(root->ch=='p')
{
len=len/4;
cacluate(root->fir,len);
cacluate(root->sec,len);
cacluate(root->thi,len);
cacluate(root->fou,len);
}
else if(root->ch=='f')
sum+=len;
*/
if(root!=NULL)
{
if(root->ch=='f')
sum+=len;
len=len/4;
cacluate(root->fir,len);
cacluate(root->sec,len);
cacluate(root->thi,len);
cacluate(root->fou,len);
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int T;
cin>>T;
while(T--)
{
int loc;
loc=0;
cin>>a;
Node *root1=creat(loc);
cin>>a;
loc=0;
Node *root2=creat(loc);
Node *root=combine(root1,root2);
sum=0;
cacluate(root,1024);
printf("There are %d black pixels.\n",sum);
}
return 0;
}
分享到:
相关推荐
正弦信号的matlab代码使用DCT和四叉树进行图像压缩 简介图像压缩:图像压缩可最大程度地减小图形文件的字节大小,而不会降低图像质量到不可接受的水平。 文件大小的减小允许在给定数量的磁盘或内存空间中存储更多...
quadtree-js, 另一个用于javascript的四叉树实现 四叉树 js这是本教程中介绍的Java方法的JavaScript四叉树实现: http://gamedev.tutsplus.com/tutorials/implementation/quick-tip-use-quadtrees-to-
Continuous LOD terrain meshing using adaptive quadtrees 770 2D surface deformation 782 Lone game developper battles physics simulator 795 Collision response : bouncy, trouncy, fun 801 Simultaneous ...
四叉树例子(几乎不大不小JavaScript库)与 。四叉树 如何使用您可以下载quadtree.js并将其包含在您的p5草图中,或通过以下CDN链接进行引用: <... </ script > 一旦包含了库,就可以创建一个具有Rectangle...
BentoMap是四叉树quadtrees的实现,用于地图标注集群和存储采用Swift开发。
4.30 Quadtrees 461 4.31 Quadrisection 478 4.32 Space-Filling Curves 485 4.33 Hilbert Scan and VQ 487 4.34 Finite Automata Methods 497 4.35 Iterated Function Systems 513 4.36 Cell Encoding 529 xxiv ...
You'll dive deep into how scripting engines encode behavior, how quadtrees and other spatial partitions optimize your engine, and how other classic design patterns can be used in games. Table of ...
生活质量简介/恢复该库实现了Quadtrees和Octrees(以Point和Region两种形式)的简单版本,该库是出于教育目的而创建的,请不要尝试在生产系统中使用它。 可以轻松实现四方树和八叉树形式的简易图书馆(点对点地区的...
QuadTrees空间细分 广相碰撞检测 分离轴定理 窄相碰撞检测 瓷砖引擎 精灵系统 渲染动画图像和纯色。 附加功能 Emscript6模块减小包装尺寸 附加模块 资产模块 加载图像,音频和json文件。 输入模块 捕获键盘输入 ...