806:Spatial Structures

版权声明:本文为博主原创文章,未经博主允许不得转载。 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;
    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(' ');
            }
            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;
}

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 64 + 5;
char g[maxn][maxn];
int n, k[10] = { 1 };
vector<int>a;

int solve_g(int l,int r,int u,int d,int cur,int p){
    int w = 1, b = 1;
    for(int i = u; i <= d; i++){
        if(!w && !b) break;
        for(int j = l; j <= r; j++){
            if(g[i][j] == '1') w = 0;
            else b = 0;
        }
    }
    if(w || b) return b ? cur : -1;
    int x = (l+r)/2, y = (u+d)/2, t;
    if((t = solve_g(l,x,u,y,cur + k[p],p+1)) > -1) a.push_back(t);
    if((t = solve_g(x+1,r,u,y,cur + 2*k[p],p+1)) > -1) a.push_back(t);
    if((t = solve_g(l,x,y+1,d,cur + 3*k[p],p+1)) > -1) a.push_back(t);
    if((t = solve_g(x+1,r,y+1,d,cur + 4*k[p],p+1)) > -1) a.push_back(t);
    return -1;
}

void solve_a(int a){
    int l = 0, r = n-1, u = 0, d = n-1;
    while(a){
        int x = (l + r) / 2, y = (u + d) / 2;
        switch(a % 5){
            case 1: r = x; d = y; break;
            case 2: l = x + 1; d = y; break;
            case 3: r = x; u = y + 1; break;
            case 4: l = x + 1; u = y + 1; break;
        }
        a /= 5;
    }
    for(int i = u; i <= d; i++){
        for(int j = l; j <= r; j++){
            g[i][j] = '*';
        }
    }
}

int main(){
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    for(int i = 1; i<10; ++i) k[i] = k[i-1]*5;
    int cnt = 0,t;
    while(scanf("%d",&n),n){
        if(cnt) printf("\n");
        printf("Image %d\n",++cnt);
        a.clear();
        memset(g,0,sizeof(g));
        if(n > 0){
            getchar();
            for(int i = 0; i<n; ++i) fgets(g[i],maxn-1,stdin);
            if((t = solve_g(0,n-1,0,n-1,0,0)) > -1) a.push_back(t);
            sort(a.begin(),a.end());
            int len = a.size();
            for(int i = 0; i < len;){
                printf("%d",a[i++]);
                if(i%12 == 0 || i == len) printf("\n");
                else printf(" ");
            }
            printf("Total number of black nodes = %d\n",len);
        }
        else{
            n = -n;
            while(1){
                scanf("%d",&t);
                if(t < 0) break;
                solve_a(t);
            }
            for(int i = 0; i < n; ++i){
                for(int j = 0; j<n; ++j)
                    if(!g[i][j]) g[i][j] = '.';
                puts(g[i]);
            }
        }
    }
    return 0;
}

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页