您的位置:首页 > 其它

uva 297 - Quadtrees

2012-07-19 11:53 519 查看



Quadtrees

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.

给出四叉树的前序遍历,计算2个树叠加后黑色的面积大小,每个节点要么为叶子节点要么必有四个子节点。

然后同时遍历2个树,只要有一个为f就增加,同时返回上一层继续遍历,

代码很臭我懂得,只要把四个指针用个数组存起来代码就可以短好多,中间复制粘贴累死了

可能有只输入

f或e的特殊数据,第一次wrong,第二次加了这种情况就对了。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 20000
int exp[8]={0,1024,256,64,16,4,1},point[max],ans;
char s[max],stack[max];
struct tree
{char data;
struct tree *one,*two,*three,*four;
}*link[max];
void work(struct tree* root,int k,int f)
{struct tree *p;
if (point[k]) p=link[k];
else  p=(struct tree*)malloc(sizeof(struct tree));
p->data=stack[k];
if ((p->data=='f')||(p->data=='e'))
{p->one=NULL; p->two=NULL; p->three=NULL; p->four=NULL;}
if (f==1) root->one=p;
if (f==2) root->two=p;
if (f==3) root->three=p;
if (f==4) root->four=p;
};
int dfs(struct tree *root,struct tree *Root,int deep,int f1,int f2)
{
if (((root->data=='f')&&(f1==1))||((Root->data=='f')&&(f2==1)))  {ans+=exp[deep];return 0;}
if ((root->one!=NULL)&&(Root->one!=NULL))  dfs(root->one,Root->one,deep+1,1,1);
if ((root->one!=NULL)&&(Root->one==NULL))  dfs(root->one,Root,deep+1,1,0);
if ((root->one==NULL)&&(Root->one!=NULL))  dfs(root,Root->one,deep+1,0,1);

if ((root->two!=NULL)&&(Root->two!=NULL))  dfs(root->two,Root->two,deep+1,1,1);
if ((root->two!=NULL)&&(Root->two==NULL))  dfs(root->two,Root,deep+1,1,0);
if ((root->two==NULL)&&(Root->two!=NULL))  dfs(root,Root->two,deep+1,0,1);

if ((root->three!=NULL)&&(Root->three!=NULL))  dfs(root->three,Root->three,deep+1,1,1);
if ((root->three!=NULL)&&(Root->three==NULL))  dfs(root->three,Root,deep+1,1,0);
if ((root->three==NULL)&&(Root->three!=NULL))  dfs(root,Root->three,deep+1,0,1);

if ((root->four!=NULL)&&(Root->four!=NULL))  dfs(root->four,Root->four,deep+1,1,1);
if ((root->four!=NULL)&&(Root->four==NULL))  dfs(root->four,Root,deep+1,1,0);
if ((root->four==NULL)&&(Root->four!=NULL))  dfs(root,Root->four,deep+1,0,1);

return 0;
}
int main()
{
struct tree *p,*root,*ROOT;
int i,j,pos,sum,l,top,f,n,k;
scanf("%d\n",&n);
for (k=1;k<=n*2;++k)
{
gets(s);  top=0;
l=strlen(s);
if (l==1) {root=(struct tree*)malloc(sizeof(struct tree));
root->data=s[0]; root->one=NULL;root->two=NULL;root->three=NULL;root->four=NULL;}
else
{
for (i=0;i<l;i++)
{++top;
stack[top]=s[i];
point[top]=0;
f=1;
while (f==1&&(top>4))
{sum=0;
for (j=top-3;j<=top;j++)
if (stack[j]!='p') ++sum;
if (sum==4)
{if (point[top-4]==1) root=link[top-4];
else   root=(struct tree*)malloc(sizeof(struct tree));
root->data='x';
work(root,top-3,1); work(root,top-2,2);
work(root,top-1,3); work(root,top,4);
top=top-4;
point[top]=1; stack[top]='x'; link[top]=root;

}
else f=0;
}
}
}

if (k%2==1) ROOT=root;
if (k%2==0) {ans=0; dfs(root,ROOT,1,1,1);printf("There are %d black pixels.\n",ans); }
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: