// uva 297 - Quadtrees
// 题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=233
// 题目大意: 每附图分成32X32,将部分染上黑色,将两附图合并,求黑色总数
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXM = 1500;
const int MAXN = MAXM * 2;
int n_case, ans;
int buf_pos;
char buf[MAXM];
int quadtree[MAXN];
void BuildTree(int);
void Init();
int GetPixels(int, int);
inline int FirstNode(int pos){
return pos << 2 | 1;
}
inline int SecondNode(int pos){
return pos << 2 | 2;
}
inline int ThirdNode(int pos){
return pos << 2 | 3;
}
inline int FourthNode(int pos){
return (pos << 2) + 4;
}
int main(){
cin >> n_case;
getchar();
while (n_case--){
Init();
gets (buf);
buf_pos = 0;
BuildTree(0);
gets(buf);
buf_pos = 0;
BuildTree(0);
ans = GetPixels(0, 32 * 32);
cout << "There are " << ans << " black pixels." << endl;
}
return 0;
}
void Init(){
memset(quadtree, 0, sizeof (quadtree));
ans = -1;
}
void BuildTree(int tree_node_pos){
if(buf[buf_pos] == 'e'){
buf_pos++;
return;
}
if(buf[buf_pos] == 'f'){
buf_pos++;
quadtree[tree_node_pos] = 1;
return;
}
buf_pos++;
quadtree[tree_node_pos] = 2;
BuildTree(FirstNode(tree_node_pos));
BuildTree(SecondNode(tree_node_pos));
BuildTree(ThirdNode(tree_node_pos));
BuildTree(FourthNode(tree_node_pos));
}
int GetPixels(int tree_node_pos, int power){
int pixels = quadtree[tree_node_pos];
if(pixels == 0 || pixels == 1){
return pixels * power;
}
else
return
GetPixels (SecondNode(tree_node_pos), power >> 2)
+ GetPixels (ThirdNode(tree_node_pos), power >> 2)
+ GetPixels (FourthNode(tree_node_pos), power >> 2)
+ GetPixels (FirstNode(tree_node_pos), power >> 2);
}
uva 297 - Quadtrees
最新推荐文章于 2020-07-28 23:12:19 发布