uva 297 Quadtrees

9 篇文章 0 订阅

https://vjudge.net/problem/UVA-297

题意:两个四叉树的合并问题

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<stack>
#include<sstream>
using namespace std;

int vis[32][32], ans = 0;
void draw(int &p, string &s, int a,int b,int w)
{
	char ch = s[p++];
	if (ch == 'p') {
		draw(p, s, a, b + w / 2, w / 2);
		draw(p, s, a, b, w / 2);
		draw(p, s, a + w / 2, b, w / 2);
		draw(p, s, a + w / 2, b + w / 2, w / 2);
	}
	if (ch == 'f') {
		for (int i = a; i < a + w; i++) {
			for (int j = b; j < b + w; j++) {
				if (!vis[i][j]) {
					vis[i][j] = 1; ans++;
				}
			}
		}
	}
}
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		ans = 0;
		memset(vis, 0, sizeof(vis));
		string s1, s2;
		cin >> s1; cin >> s2;
		int p = 0;
		draw(p, s1, 0, 0, 32);
		p = 0;
		draw(p, s2, 0, 0, 32);
		printf("There are %d black pixels.\n", ans);
	}
	return 0;
}

非递归,常规思路 

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<map>
#include<set>
#include<stack>
#include <sstream>
//#include <Eigen/Dense>
//#include <Eigen/Geometry>
//using namespace Eigen;

using namespace std;

int idx = -1;
int dfs(string &s,int l)
{
	idx++;
	char c = s[idx];
	if (c == 'e')
		return 0;
	if (c == 'f')
		return 64 * pow(4, 2 - l);
	else
		return dfs(s,l + 1) + dfs(s, l + 1) + dfs(s, l + 1) + dfs(s, l + 1);
}


int main() {
	int n;
	cin >> n;
	while (n--)
	{
		idx = -1;
		string s1, s2, s3;
		cin >> s1;
		cin >> s2;
		int i = 0, j = 0;
		while (i < s1.size() || j < s2.size())
		{
			if ((s1[i] == 'p' && s2[j] == 'p') || (s1[i] == 'e' && s2[j] == 'e')|| (s1[i] == 'f' && s2[j] == 'f'))
			{
				s3.push_back(s1[i]);
				i++; j++;
			}
			else if (s1[i] == 'p' && s2[j] == 'f')
			{
				s3.push_back('f');
				i += 5;
				j++;
			}
			else if (s1[i] == 'f' && s2[j] == 'p')
			{
				s3.push_back('f');
				j += 5;
				i++;
			}
			else if (s1[i] == 'p' && s2[j] == 'e')
			{
				s3.push_back(s1[i]); s3.push_back(s1[i + 1]); s3.push_back(s1[i + 2]); s3.push_back(s1[i + 3]); s3.push_back(s1[i + 4]);
				i += 5;
				j++;
			}
			else if (s1[i] == 'e' && s2[j] == 'p')
			{
				s3.push_back(s2[j]); s3.push_back(s2[j + 1]); s3.push_back(s2[j + 2]); s3.push_back(s2[j + 3]); s3.push_back(s2[j + 4]);
				j += 5;
				i++;
			}
			else if (s1[i] == 'e' && s2[j] == 'f')
			{
				s3.push_back('f');
				j++;
				i++;
			}
			else if (s1[i] == 'f' && s2[j] == 'e')
			{
				s3.push_back('f');
				j++;
				i++;
			}
		}
		printf("There are %d black pixels.\n", dfs(s3, 0));
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值