~~~题目链接~~
题目大意:根据题目的要求可以把矩形(32*32)转换成4叉树, 现在给出2个矩形, 2个矩形相加求出第三个矩形,然后求第三个矩形黑色像素所占的面积。
code:
#include <iostream>
using namespace std;
typedef struct node
{
char ch;
struct node *nt[4];
}Node;
int ans = 0;
void build(int &cur, Node *r, string &s)
{
int flag = 0;
r->ch = s[cur++];
for(int j = 0; j<4 && cur<s.size(); j++)
{
r->nt[j] = new Node;
if(s[cur] == 'p')
{
build(cur, r->nt[j], s);
continue;
}
r->nt[j]->ch = s[cur++];
}
}
void query(Node *r1, Node *r2, int cur)
{
if(r1->ch == 'p' && r2->ch == 'p')
for(int i = 0; i<4; i++)
query(r1->nt[i], r2->nt[i], cur*2);
else if(r1->ch == 'f' || r2->ch == 'f')
ans += (32/cur)*(32/cur);
else if(r1->ch == 'e' && r2->ch == 'p')
for(int i = 0; i<4; i++)
query(r1, r2->nt[i], cur*2);
else if(r1->ch == 'p' && r2->ch == 'e')
for(int i = 0; i<4; i++)
query(r1->nt[i], r2, cur*2);
}
int main()
{
int t = 0, cur = 0;
string s1, s2;
cin>>t;
while(t--)
{
cin>>s1>>s2;
ans = 0;
Node *r1 = new Node;
Node *r2 = new Node;
cur = 0;
build(cur, r1, s1);
cur = 0;
build(cur, r2, s2);
query(r1, r2, 1);
cout<<"There are "<<ans<<" black pixels."<<endl;
}
return 0;
}