原题:
file:///D:/Programming/DevC++/2018.5/UVa806%20Spatial%20Structures.pdf
题目简述:用四分树表示黑白图片,NW,NE,SW,SE四块分别编号为1,2,3,4,用五进制序列表示从根到黑色节点的路径,转换为十进制数存储。要求输入黑白图片,输出四分树;或输入四分树,输出图片。
分析:给定树转换为图片的部分:对于每个五进制序列,递归访问图片直到到达目标块,涂黑;
给定图片转换为树的部分:
递归执行四分图片的操作,记录下访问路径,当出现全黑块时,保存路径并return。
代码:
//Spatial Structures
//My Solution. Yhq
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#define maxn 1<<9
using namespace std;
char pic[maxn][maxn];
int size;
vector<int> vec[maxn];
vector<int> tree;
int vec_cnt=0;
void reset() {
tree.clear();
vec_cnt=0;
memset(pic,'.',sizeof(pic));
for (int i=0; i<maxn; ++i) vec[i].clear();
}
void trans(int n) {
while (n>0) {
int p=n%5;
n=n/5;
vec[vec_cnt].push_back(p);
}
vec_cnt++;
}
void to_pic_dfs(int cnt, int row, int col, int sz, int dep) {
if (dep==vec[cnt].size()) {
for (int i=row; i<row+sz; ++i)
for (int j=col; j<col+sz; ++j)
pic[i][j]='*';
return;
}
switch (vec[cnt][dep]) {
case 1:
to_pic_dfs(cnt, row, col, sz/2, dep+1);
break;
case 2:
to_pic_dfs(cnt, row, col+sz/2, sz/2, dep+1);
break;
case 3:
to_pic_dfs(cnt, row+sz/2, col, sz/2, dep+1);
break;
case 4:
to_pic_dfs(cnt, row+sz/2, col+sz/2, sz/2, dep+1);
break;
}
}
void to_pic() {
for (int i=0; i<vec_cnt; ++i) to_pic_dfs(i, 0, 0, size, 0);
}
void print_pic() {
for (int i=0; i<size; ++i)
for (int j=0; j<size; ++j) {
cout<<pic[i][j];
if (j==size-1) cout<<endl;
}
}
bool isfull (int row, int col, int sz) {
for (int i=row; i<row+sz; ++i)
for (int j=col; j<col+sz; ++j)
if (pic[i][j]=='.') return false;
return true;
}
void to_tree_dfs (int row, int col, int sz, int step, int tmp) {
if (sz/2==0) return;
if (isfull(row,col,sz/2)) {
tree.push_back(tmp+pow(5,step)*1);
} else {
to_tree_dfs(row, col, sz/2, step+1, tmp+pow(5,step)*1);
}
if (isfull(row,col+sz/2,sz/2)) {
tree.push_back(tmp+pow(5,step)*2);
} else {
to_tree_dfs(row, col+sz/2, sz/2, step+1, tmp+pow(5,step)*2);
}
if (isfull(row+sz/2,col,sz/2)) {
tree.push_back(tmp+pow(5,step)*3);
} else {
to_tree_dfs(row+sz/2, col, sz/2, step+1, tmp+pow(5,step)*3);
}
if (isfull(row+sz/2,col+sz/2,sz/2)) {
tree.push_back(tmp+pow(5,step)*4);
} else {
to_tree_dfs(row+sz/2, col+sz/2, sz/2, step+1, tmp+pow(5,step)*4);
}
}
void print_tree() {
sort(tree.begin(), tree.end());
if (tree.empty()) cout<<"Total number of black nodes = 0"<<endl;
else {
for (int i=0; i<tree.size(); ++i) {
cout<<tree[i];
if (i<tree.size()-1) cout<<" ";
else cout<<endl;
}
}
}
int main () {
int counter=0;
while(cin>>size) {
if (size==0) return 0;
counter++;
cout<<"Image "<<counter<<endl;
reset();
if (size<0) {
size=-size;
int num;
while (cin>>num) {
if (num==-1) break;
if (num==0) {
for (int i=0; i<size; ++i)
for (int j=0; j<size; ++j)
pic[i][j]='*';
} else trans(num); //转换成“路径表示序列 ”存入vector[vec_cnt]
}
to_pic();
print_pic();
} else {
char ch;
for (int i=0; i<size; ++i)
for (int j=0; j<size; ++j) {
cin>>ch;
if (ch=='1') pic[i][j]='*';
}
if (isfull(0,0,size)) tree.push_back(0);
else to_tree_dfs(0, 0, size, 0, 0);
print_tree();
}
cout<<endl;
}
return 0;
}