您的位置:首页 > 其它

UVA/6-11/ 297 - Quadtrees

2017-03-06 20:31 357 查看
四分树乖巧

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

// 图片宽度
const int len = 32;
// 存储图片信息的二维数组
int img[len][len];
// 像素点数
int ans;

// 统计像素点数
// (row,column)代表像素块左上角
// width代表像素块边长
// 以这三个参数就能将一个像素块涂黑
// 2 1
// 3 4
void quadTreePlus(const string s, int &pos, int row, int column, int width) {
// 获取节点类型
char ch = s[pos++];
// p 表示拥有子节点
if(ch == 'p') {
// 节点1
quadTreePlus(s, pos, row, column + width / 2, width / 2);
// 节点2
quadTreePlus(s, pos, row, column , width / 2);
// 节点3
quadTreePlus(s, pos, row + width / 2, column , width / 2);
// 节点4
quadTreePlus(s, pos, row + width / 2, column + width / 2, width / 2);
} else if(ch == 'f') {
// 只画黑像素
// 画一个以(row,column)为左上角,边长是width的正方形像素块
for(int i = row; i < row + width; i++) {
for(int j = column; j < column + width; j++) {
if(img[i][j] == 0) {
// 涂黑
img[i][j] = 1;
// 计数
ans++;
}
}
}
}
}

int main() {
int N;
cin >> N;
string s;
while(N--) {
memset(img, 0, sizeof(img));
ans = 0;
// 处理相加的两个四叉树
for(int i = 0; i < 2; i++) {
cin >> s;
// 节点当前位置
int pos = 0;
quadTreePlus(s, pos, 0, 0, len);
//draw(s, pos, 0, 0, len);
}
cout << "There are " << ans << " black pixels." << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: