四分树是如果,全黑就f,如果全白就e,有黑有白就f 同时描述f的情况
描述顺序是
2 1
3 4
这样递归下去
现在给你两个四分树,把这两个四分树合并,最合并之后黑点的个数
直接根据四分树的定义把树建立在g数组上面
核心函数dfs就是建树过程
x,y表示左上顶点,ww表示方块宽度
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 32 + 9;
bool g[maxn][maxn];
string s;
int p;
int ans;
void dfs(int x, int y, int ww) {
char ch = s[p++];
if (ch == 'p') {
int w = ww / 2;
dfs(x, y + w, w);
dfs(x, y , w);
dfs(x + w, y , w);
dfs(x + w, y + w, w);
return;
}
if (ch == 'f') {
for (int i = x; i < x + ww; i++)
for (int j = y; j < y + ww; j++)
if (!g[i][j]) {
g[i][j] = true;
ans++;
}
}
}
void solve() {
memset(g, 0, sizeof(g));
p = 0;
ans = 0;
cin >> s;
dfs(0, 0, 32);
cin >> s;
p = 0;
dfs(0, 0, 32);
printf("There are %d black pixels.\n", ans);
}
int main() {
freopen("input", "r", stdin);
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
solve();
}
}
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int len = 32;
const int maxn = 1024;
char s[maxn];
int buf[len][len], cnt;
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 (buf[i][j] == 0) {
buf[i][j] = 1;
cnt++;
}
}
}
void solve() {
memset(buf, 0, sizeof(buf));
cnt = 0;
for (int i = 0; i < 2; i++) {
scanf("%s", s);
int p = 0;
draw(s, p, 0, 0, len);
}
printf("There are %d black pixels.\n", cnt);
}
int main() {
freopen("input","r",stdin);
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++)
solve();
}