版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/81592363
Spatial Structures
#include<bits/stdc++.h>
using namespace std;
const int maxn = 64 + 5;
int n,len;
int seq[maxn*maxn];
char img[maxn][maxn];
struct node{
char c = 0;
int cnt = 4;
int path[5] = {0};
node* dad = NULL;
node* nw = NULL;
node* ne = NULL;
node* sw = NULL;
node* se = NULL;
};
node* nodes[maxn*maxn];
bool judge(int x1,int y1,int x2,int y2)
{
char c = img[x1][y1];
for(int i = x1;i <= x2;i++){
for(int j = y1;j <= y2;j++){
if(img[i][j] != c) return false;
}
}
return true;
}
void build_tree(node* root,int x1,int y1,int x2,int y2)
{
if(judge(x1,y1,x2,y2)){
root->c = img[x1][y1];
if(root->c == '1') nodes[len++] = root;
return;
}
node* nw = new node;
node* ne = new node;
node* sw = new node;
node* se = new node;
int* p = root->path;
for(int i = 4;i > 0;i--){
if(!p[i]) continue;
int t = p[i];
nw->path[nw->cnt--] = t;
ne->path[ne->cnt--] = t;
sw->path[sw->cnt--] = t;
se->path[se->cnt--] = t;
}
nw->path[nw->cnt--] = 1;
ne->path[ne->cnt--] = 2;
sw->path[sw->cnt--] = 3;
se->path[se->cnt--] = 4;
root->ne = ne; ne->dad = root;
root->sw = sw; sw->dad = root;
root->se = se; se->dad = root;
root->nw = nw; nw->dad = root;
build_tree(nw,x1,y1,(x1+x2)/2,(y1+y2)/2);
build_tree(ne,x1,(y1+y2)/2+1,(x1+x2)/2,y2);
build_tree(sw,(x1+x2)/2+1,y1,x2,(y1+y2)/2);
build_tree(se,(x1+x2)/2+1,(y1+y2)/2+1,x2,y2);
}
int Pow(int a,int b)
{
int n = 1;
while(b--) n *= a;
return n;
}
int fTot(int* p)
{
int n = 0,i = 4,j = 0;
while(i >= 0 && p[i]){
n += p[i]*Pow(5,j);
i--; j++;
}
return n;
}
void tTof(int n,int* p)
{
int i = 0,j = 4;
for(;i < 5;i++){
if(n >= Pow(5,j)){
p[i] = n/Pow(5,j);
n -= p[i]*Pow(5,j);
}
j--;
}
}
void encoding()
{
len = 0;
node* root = new node;
build_tree(root,0,0,n-1,n-1);
for(int i = 0;i < len;i++){
int *p = nodes[i]->path;
seq[i] = fTot(p);
}
sort(seq,seq + len);
}
void draw(int x1,int y1,int x2,int y2)
{
for(int i = x1;i <= x2;i++){
for(int j = y1;j <= y2;j++){
img[i][j] = '*';
}
}
}
void decoding()
{
memset(img,0,sizeof(img));
for(int i = 0;i < len;i++){
int p[5] = {0};
tTof(seq[i],p);
int x1 = 0,y1 = 0,x2 = n-1,y2 = n-1;
for(int j = 4;j >= 0 && p[j];j--){
if(p[j] == 1){ x2 = (x1+x2)/2; y2 = (y1+y2)/2; }
else if(p[j] == 2){ y1 = (y1+y2)/2+1; x2 = (x1+x2)/2; }
else if(p[j] == 3){ x1 = (x1+x2)/2+1; y2 = (y1+y2)/2; }
else{ x1 = (x1+x2)/2+1; y1 = (y1+y2)/2+1; }
}
draw(x1,y1,x2,y2);
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(!img[i][j]) img[i][j] = '.';
}
}
}
int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
int cnt = 0;
while(scanf("%d",&n) && n){
if(cnt) putchar('\n');
printf("Image %d\n",++cnt);
if(n > 0){
for(int i = 0;i < n;i++) scanf("%s",img[i]);
encoding();
for(int i = 0;i < len;i++){
printf("%d",seq[i]);
if((i+1)%12 == 0 || i == len-1) putchar('\n');
else putchar(' ');
}
// for(int i = 0;i < len;i += 12){
// int j = 0;
// for(;j < 11 && i+j < len-1;j++) printf("%d ",seq[i+j]);
// printf("%d\n",seq[i+j]);
// }
printf("Total number of black nodes = %d\n",len);
}
else{
n = -n;
len = 0;
while(scanf("%d",&seq[len]) && seq[len] != -1) len++;
decoding();
for(int i = 0;i < n;i++) puts(img[i]);
}
}
return 0;
}