题目链接:https://vjudge.net/problem/UVA-297
思路:四分数不同于二分树,可以直接按照先序遍历的方式建树。
Code:
#include <iostream>
#include <string.h>
using namespace std;
const int AX = 1024+6;
const int px = 32;
char s[AX];
int res;
int mp[AX][AX];
void draw( int& p , int r , int c , int w ){
char ch = s[p++] ;
if( ch == 'p' ){
draw( p , r , c + w / 2 , w/2 );
draw( p , r , c , w/2 );
draw( p , r + w / 2 , c , w/2 );
draw( 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( !mp[i][j] ){
mp[i][j] = 1;
res ++ ;
}
}
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int T;
cin >> T;
while( T-- ){
res = 0;
memset(mp,0,sizeof(mp));
for( int i = 0 ; i < 2 ; i++ ){
cin >> s;
int p = 0;
draw( p , 0 , 0 , px );
}
cout << "There are "<< res << " black pixels." << endl;
}
return 0;
}