uva_297 quadtrees
来源:互联网 发布:滤镜软件 编辑:程序博客网 时间:2024/05/07 12:09
/* *题目要求:输入一串pef的字符,表示将1024个像素向下逐级分成四叉树的形式 *f表示填满像素,计算所有被填像素的量 *两次建树,第二次覆盖第一次 *遍历统计结果 */#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int sum;//tree nodetypedef struct TreeNode{ char ch; TreeNode *first,*second,*third,*four;}TreeNode;//Creat treevoid CreatTree(TreeNode **node){ if((*node)==NULL) { (*node) = new TreeNode; (*node)->first = NULL; (*node)->second = NULL; (*node)->third= NULL; (*node)->four = NULL; } char ch;ch = getchar(); //测试数据中间有换行,丢弃 if(ch == 10) ch =getchar(); int judge = 0; //有孩子节点的情况 if((*node)->ch == 'p'||(*node)->ch == 'f') { judge = 1; } if(ch == 'f') { (*node)->ch = ch; } else if(ch == 'p') { if((*node)->ch!='f') (*node)->ch = ch; CreatTree(&((*node)->first)); CreatTree(&((*node)->second)); CreatTree(&((*node)->third)); CreatTree(&((*node)->four)); } else if(ch =='e'&&!judge) { (*node)->ch = ch; }}//traver treevoid pre_traver(TreeNode const *node,int n){ //cout<<node->ch; if(node->ch == 'e'||node->ch == 'f'||!node) { if(node->ch == 'f') sum+=n; return ; } pre_traver(node->first,n/4); pre_traver(node->second,n/4); pre_traver(node->third,n/4); pre_traver(node->four,n/4);}int main(){// freopen("test1.in","r",stdin);// freopen("test1.out","w",stdout); int T; scanf("%d",&T); for(int i = 0;i<T;i ++) { sum = 0; getchar(); TreeNode *(root) = NULL; CreatTree(&root); sum = 0; getchar(); CreatTree(&root); pre_traver(root,1024); printf("There are %d black pixels.\n",sum);//// pre_traver(root);// cout<<endl; } return 0;}