ACM 四叉树 Quadtrees

这个是昨天训练落下的..今早补得..


UVA 297 Quadtrees




题目大意:p是分叉,f是涂色,e是空白,给出一个32*32的方块,求填涂面积。(四叉就是四分)

我的是用递归,把画的过程模拟出来..

#include <stdio.h>
#include <string.h>
const int len=32;
const int maxn=1024+10;
char s[maxn];
int map[len][len],sum;
void draw(const char* s,int &p,int r,int c,int w)
{
	char ch=s[p++];
	if(ch=='p')
	{
		draw(s,p,r,c+w/2,w/2);
		draw(s,p,r,c,w/2);
		draw(s,p,r+w/2,c,w/2);
		draw(s,p,r+w/2,c+w/2,w/2);
	}
	else if(ch=='f')
	{
		for(int i=r;i<r+w;i++)
		{
			for(int j=c;j<c+w;j++)
			{
				if(map[i][j]==0)
				{
					map[i][j]=1;
					sum++;
				}
			}
		}
	}
}
int main()
{
	int n,i,j,p;
	scanf("%d",&n);
	while(n--)
	{
		sum=0;
		memset(map,0,sizeof(map));
		for(i=0;i<2;i++)
		{
			scanf("%s",s);
			p=0;
			draw(s,p,0,0,len);
		}
		printf("There are %d black pixels.\n",sum);
	}
}

下面是学姐用队列写的...

#include<stdio.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
struct mm
{
	int x,y,c;
}p,q,b,w;
queue<mm> add(char *a)
{
	b.c=2,w.c=1;
	stack<mm>h;
	queue<mm>f;
	int i,s=strlen(a),j;
	p.x=0,p.y=1,p.c=0;
	h.push(p);
	for(i=0;i<s;i++)
	{
		if(a[i]=='p')
		{
			while(a[i]=='p')
			{
				if(!h.empty())
				{
					p=h.top();
					h.pop();
					for(j=4;j>=1;j--)
					{
						q.x=p.x+1;
						q.y=4*p.y+j-3;
						q.c=0;
						h.push(q);
					}
				}
				i++;
			}
		}
		if(a[i]=='e')
		{
			p=h.top();
			h.pop();
			p.c=1;
			for(j=0;j<1024/pow(4,p.x);j++)
			f.push(w);
		}
		if(a[i]=='f')
		{
			p=h.top();
			h.pop();
			p.c=2;
			for(j=0;j<1024/pow(4,p.x);j++)
			f.push(b);
		}
	}
	return f;
}
int main()
{
	int t,i,s;
	char a[5000],b[5000];
	queue<mm>f,g;
	scanf("%d",&t);
	while(t--)
	{
		s=0;
		scanf("%s%s",a,b);
		f=add(a);
		g=add(b);
		while(!f.empty())
		{
			p=f.front();f.pop();
			q=g.front();g.pop();
			if(p.c==2||q.c==2)
			s++;
		}
		//printf("%d %d\n",f.size(),g.size());
		printf("There are %d black pixels.\n",s);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值