(递归)【UVA-297】Quadtrees

简单递归,忘记初始化折腾了半天,感觉英文题面也好难看懂啊。。

先确定第一棵树,将黑的数量统计并标记,第二棵数直接覆盖就好了,已经标记成黑色的就不计入。

/*
* @Author: Samson
* @Date:   2018-06-08 22:53:16
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-09 00:01:02
*/
//   @URL : https://vjudge.net/problem/UVA-297
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

char s[1050];
int buf[33][33],ans;

void draw(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(!buf[i][j])
					{buf[i][j] = 1; ++ans;}
		}
	}
}

int main(void)
{
	int T;
	cin>>T;
	while(T--)
	{
		memset(buf,0,sizeof buf);
		int t = 2;
		ans = 0;
		while(t--)
		{
			scanf("%s",s);
			int p = 0;
			draw(s,p,1,1,32);
		}
		printf("There are %d black pixels.\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/suiguia/article/details/80629464