UVA 806 Spatial Structures

本文介绍了一种使用四叉树表示黑白图像的方法,并详细解释了两种操作:一是从图像生成黑色像素路径,二是从路径重建图像。文章提供了完整的代码实现,包括递归分割和路径转换等功能。


题意:用四叉树可以表示黑白图像,对于一个黑白图像等分四部分,左上、右上、左下、右下。如果一个区域同为一种颜色,那么就不再分叉。现在从根节点开始,走左上记1、右上记2、左下记3、右下记4。一个节点的路径就可以用这些数连起来构成一个五进制的数。输入有两种情况,一种是给n*n的0/1点阵,输出所有黑色节点的路径(10进制);一种是给出黑色节点的路径(10进制),输出n*n的点阵。


思路:对于第一种情况,我们用dfs递归并记录路径,先是(1,1)为左上角开头的n*n部分点阵,我们看看这个点阵如果是纯色(如果是黑色就把当前路径转化一下放进vector里)就不继续递归,否则要继续递归4个部分。输出注意12个一换行。

第二种情况,每次读入一个黑色节点的路径,我们将其转化后按照这个路径去递归(类似于字典树去寻找节点),到达指点后将这个节点管辖范围内的区域全部染黑。最后输出即可。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
const int maxn = 70;
int n;

int a[maxn][maxn];
vector<int> ans;

int change(int x,int type)
{
    int t = 0;
    if( type == 1 )
    {
        while(x)
        {
            t = t * 5 + x % 10;
            x/=10;
        }
        return t;
    }
    while(x)
    {
        t = t * 10 + x % 5;
        x/=5;
    }
    x = 0;
    while(t)
    {
        x = x * 10 + t % 10;
        t/=10;
    }
    return x;

}
void dfs(int x,int y,int k,int t,int type) //type = 0 是-n的情况 type  = 1是n的情况
{
    if ( type == 1 )
    {
        int sum = 0;
        rep(i,x,x+k-1)
            rep(j,y,y+k-1) sum+=a[i][j];
        if ( sum == k*k ){
                //printf("%d %d %d : %I64d\n",x,y,k,change(t,1));
        ans.push_back( change(t,1) );return;}
        if ( sum == 0 ) return;
        dfs(x,y,k>>1,t*10+1,type);
        dfs(x,y+(k>>1),k>>1,t*10+2,type);
        dfs(x+(k>>1),y,k>>1,t*10+3,type);
        dfs(x+(k>>1),y+(k>>1),k>>1,t*10+4,type);
    }
    else
    {
        if ( !t ) //到达指点节点,范围区域染黑
        {
            rep(i,x,x+k-1)
            rep(j,y,y+k-1) a[i][j] = 1;
            return;
        }
        int yu = t % 10;
        if ( yu == 1 ) dfs(x,y,k>>1,t/10,type);
        if ( yu == 2 ) dfs(x,y+(k>>1),k>>1,t/10,type);
        if ( yu == 3 ) dfs(x+(k>>1),y,k>>1,t/10,type);
        if ( yu == 4 ) dfs(x+(k>>1),y+(k>>1),k>>1,t/10,type);
    }
}

void output()
{
    rep(i,1,n)
    {
        rep(j,1,n) putchar(  a[i][j]==0?'.':'*' );
        putchar('\n');
    }
}

int main()
{
    int kase = 0;
    while(cin>>n)
    {
        if ( 0 == n ) break;
        if( kase ) puts("");
        printf("Image %d\n",++kase);
        ans.clear();
        if ( n<0 )
        {
            n = -n;
            Clean(a,0);
            int temp;
            while( scanf("%d",&temp)==1 && temp!=-1 ) dfs(1,1,n,change(temp,0),0);
            output();
        }
        else
        {
            rep(i,1,n)
            {
                getchar();
                rep(j,1,n) a[i][j] = getchar() - '0';
            }
            dfs(1,1,n,0,1);
            sort(ans.begin(),ans.end());
            int k = ans.size();
            if ( k )
            {
                rep(i,0,k-1)
                    printf("%d%c",ans[i],( i%12==11 || i==k-1 )?'\n':' ');
            }
            printf("Total number of black nodes = %d\n",l);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值