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;
}