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;
}
#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;
}
相关文章推荐
- Uva297 Quadtrees【递归建四分树】【例题6-11】
- UVa297 例题6-11 四分树(Quadtrees)
- 例题6-11 四分树(Quadtrees, UVa 297)
- 6-11 四分树(Quadtrees, UVa 297)
- 例题6-11 UVa297 Quadtrees(四分树)
- uva297 - Quadtrees 入门经典II 第六章 数据结构基础 例题6-11
- 297 - Quadtrees (四叉树)
- uva 297 quadtrees——yhx
- UVa 297 Quadtrees(模拟&四分树)
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
- UVA 297 Quadtrees (4叉树)
- uva297 Quadtrees (树的重建)
- 297 - Quadtrees
- UVA 297 - Quadtrees
- UVA 297 - Quadtrees
- UVA - 297Quadtrees(四分图)
- dfs uva-297-Quadtrees
- 297 - Quadtrees
- 297 - Quadtrees*****
- UVA 297 Quadtrees