uva 297 Quadtrees

737 篇文章 0 订阅

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 tex2html_wrap_inline34 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.



题目大意:输入一个n,做为测试组数。对图每一测试组,有两个字符串。字符串由‘p'(代表节点),’f'(代表填充满),‘e'(代表空)。每个p后面所跟的4个字符(有可能多个,如果在4个字符中有’p'),为该‘p'的4个子节点的状态。

   由两个字符串可以生成两个题目中的像素图,取像素图的并集,输出所得像素图的像素大小。

解题思路:将两个字符串合并(根据四叉树的特点,考虑不同的情况),有生成的字符串求出最后的像素值。


#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
#define N 10000

int sum;				//记录和像素。
char *move;				//遍历字符串求像素时的辅助指针。

void build(int n);		//通过递归求解。

int cut(char *t,int f)	//需要多个字符拷贝或抛弃时,计算个数,返回值为移动后的下标,与原先坐标相减即为个数。
{
    for(int i=0; i<4; i++)
    {
        f++;
        if(t[f]=='e'||t[f]=='f')
            continue;
        else if(t[f]=='p')
            f=cut(t,f);
    }
    return f;
}

int main()
{
	char str1[N],str2[N],tem[N];
	int n;
	cin>>n;
	while(n--)
	{
		//Init.
		memset(str1,0,sizeof(str1));
		memset(str2,0,sizeof(str2));
		memset(tem,0,sizeof(tem));

		cin>>str1>>str2;

		//Count.
		int num1=0,num2=0,num_t=0;
		while(1)
		{
			if(str1[num1]=='\0'&&str2[num2]=='\0')	break;		//两个字符串均遍历过后跳出循环。
			
			if(str1[num1]=='p'&&str2[num2]=='p')			
			{
				num1++;
				num2++;
				tem[num_t++]='p';
			}
			else if(str1[num1]=='p'&&str2[num2]=='f')
			{
				num1=cut(str1,num1)+1;
				num2++;
				tem[num_t++]='f';
			}
			else if(str1[num1]=='f'&&str2[num2]=='p')
			{
				num1++;
				num2=cut(str2,num2)+1;
				tem[num_t++]='f';
			}
			else if(str1[num1]=='p'&&str2[num2]=='e')
			{
				int k=cut(str1,num1)+1-num1;
				strncpy(tem+num_t,str1+num1,k);
				num1+=k;
				num2++;
				num_t+=k;
			}
			else if(str1[num1]=='e'&&str2[num2]=='p')
			{
				int k=cut(str2,num2)+1-num2;
				strncpy(tem+num_t,str2+num2,k);
				num1++;
				num2+=k;
				num_t+=k;
			}
			else if(str1[num1]=='e'&&str2[num2]=='f')
			{
				num1++;
				num2++;
				tem[num_t++]='f';
			}
			else if(str1[num1]=='f'&&str2[num2]=='e')
			{
				num1++;
				num2++;
				tem[num_t++]='f';
			}
			else if(str1[num1]=='f'&&str2[num2]=='f')
			{
				num1++;
				num2++;
				tem[num_t++]='f';
			}
			else if(str1[num1]=='e'&&str2[num2]=='e')
			{
				num1++;
				num2++;
				tem[num_t++]='e';
			}
			else if(str1[num1]=='\0')				//当有一个字符串先遍历完,另一串剩余字符可直接拷贝。
			{
				while(str2[num2]!='\0')
					tem[num_t++]=str2[num2++];
			
			}
			else if(str2[num2]=='\0')
			{
				while(str1[num1]!='\0')
					tem[num_t++]=str1[num1++];
			}
		
		}
	
		move=tem;						//move指针指向和字符串首部,定义成全局变量移动指针进行遍历。
		sum=0;
		build(0);
		cout<<"There are "<<sum<<" black pixels."<<endl;
	}
return 0;}

void build(int n)			//n为记录当前层数,用于计算像素值。
{
	int i;

	for(i = 0;i < 4; i++)
	{
		if(*move=='p')
		{	move++;
			build(n+1);
		}
		else if(*move=='f')
		{	sum+=pow(4,5-n);		//该点的像素值为4的5-n次方。
			move++;
		}
		else
			move++;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值