UVA 10318 简单dfs


#include<cstdio>

#include<cstring>
#include<iostream>
using namespace std;
const int MAXN= 6;
const int inf=1<<30;
int n, m,cas=1;
bool pat[3][3];
int  path[MAXN*MAXN], mat[MAXN], mm;
void read()
{
    memset(mat,0, sizeof(mat));
    for(int i=0; i<3; ++i)
    {
        for(int j=0; j<3; ++j)
        {
            pat[i][j]=0;
            pat[i][j] = getchar()=='*'?1:0;
        }


        getchar();
    }
}
int ok(int r)
{
    if(mat[r]==(1<<m)-1)return 1;
    return 0;
}
int check()
{
    if(n>1 && ok(n-1) && ok(n-2) ||
       n==1 && ok(0))
        return 1;
    return 0;
}
void cover(int x,int y)
{
    int i,j;
    for(i=x-1;i<=x+1;i++)
    {
        if(i<0||i>=n)
            continue;
        for(j=y-1;j<=y+1;j++)
        {
            if(j<0||j>=m)
                continue;
            if(pat[i-x+1][j-y+1])
                mat[i]^=(1<<j);
        }
    }
}
int dfs(int cur,int step)
{
    int r=cur/m,c=cur-r*m;
    if(r>=2&&!ok(r-2))return 0;
    if(cur==m*n)
    {
        if(check())
        {
            mm=step;
            return 1;
        }
        return 0;
    }
    if(dfs(cur+1,step))return 1;
    cover(r,c);
    path[step]=cur+1;
    if(dfs(cur+1,step+1))return 1;
    cover(r,c);
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m),(n&&m))
    {
        getchar();
        memset(path,0,sizeof(path));
        read();
        mm=inf;
        printf("Case #%d\n", cas++);
        if(dfs(0,0))
        {
            cout<<path[0];
            for(int i=1;i<mm;i++)
            {
                cout<<" "<<path[i];
            }cout<<endl;
        }else   puts("Impossible.");
    }
    return 0;
}
/*
2 3
**.
.*.
*..
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值