[2012山东省第三届ACM大学生程序设计竞赛]——Mine Number SDUT 2410 DFS+剪枝

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2410

         

数字所代表的是 上下左右与该点   五个方向的雷数量。

题目会给出数字,让你确定 雷的分布图,每个数据只输出一个解。

DFS,深度优先搜索。

大体的思路方向是:

从0,0开始往后判断,每个点是否放雷,依据就是周围的数字(上下左右)是否有0的情况,有0就不放雷。

放雷后就要将五个方向的数字减1,然后继续往后判断。

这是主要的判断,但是显然需要大的前提来 剪掉大半棵树。

→首先将第一行枚举,然后每一行根据上一行状态来做:

如果上一行同位置数字为0,则该点不放雷。

如果上一行同位置数字为1,则该点必须放雷,此时判断四周是否有0的情况,没有则放雷,有则回溯。

如果上一行同位置数字不为0或者1,则回溯。

判断到最后一行,需要将最后一行数组判断,是否全为0,是则输出结果,不是则回溯。

原文:https://blog.csdn.net/lttree/article/details/24630865 
 

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
int mapp[21][21];
char s[21][21],str[21][21];
int a[5][2]={
    0,0,
    0,-1,
    0,1,
    1,0,
    -1,0
};
int n,m,vis;
bool loca(int x,int y)
{
    if(x<0 || x>=n || y<0 || y>=m)
        return false;
    return true;
}
bool check(int x,int y)  //检验是否能放雷
{
    for(int i=0;i<5;i++)
    {
        int xx = x+a[i][0];
        int yy = y+a[i][1];
        if(loca(xx,yy) && mapp[xx][yy]<=0)
            return false;
    }
    return true;
}
bool finally( )  //判断最后一行是否全为0
{
    for(int i=0;i<m;i++)
    {
        if(mapp[n-1][i]!=0)
            return false;
    }
    return true;
}
void change(int x,int y)  //如果放雷,五个点数字减 1
{
    for(int i=0;i<5;i++)
    {
        int xx = x+a[i][0];
        int yy = y+a[i][1];
        if(loca(xx,yy))
            mapp[xx][yy]--;
    }
}
void m_back(int x,int y)  //如果该地原来放雷,但是应该不放,回溯,五个点数字加1
{
    for(int i=0;i<5;i++)
    {
        int xx = x+a[i][0];
        int yy = y+a[i][1];
        if(loca(xx,yy))
            mapp[xx][yy]++;
    }
}
void dfs(int x,int y)
{
   // printf("%d %d\n",x,y);
    if(vis)  //找到结果
        return ;
    if(y==m)
    {
        dfs(x+1,0);
        return ;
    }
    if(x==n)
    {
        if(finally())
        {
            vis = 1;
            for(int i=0;i<n;i++)
                printf("%s\n",str[i]);
        }
        return ;
    }
    if(x==0)   //首先从第一行要进行枚举,不冲突即可
    {
        if(check(x,y))
        {
            change(x,y);
            str[x][y] = '*';
            dfs(x,y+1);
            m_back(x,y);
        }
        str[x][y] = '.';
        dfs(x,y+1);
    }
    else  //其余行根据上一行来判断
    {
        if(mapp[x-1][y]==1)  //上一行为1,此处必须放雷
        {
            if(loca(x,y))
            {
                change(x,y);
                str[x][y] = '*';
                dfs(x,y+1);
                m_back(x,y);
            }
        }
        else if(mapp[x-1][y]==0) //上一行为0,此处不能放雷
        {
            str[x][y] = '.';
            dfs(x,y+1);
        }
    }

}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        scanf("%d%d ",&n,&m);
        memset(mapp,0,sizeof(mapp));
        memset(str,0,sizeof(str));
        vis = 0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&s[i][j]);
                mapp[i][j] = s[i][j]-'0';
            }
            getchar();
        }
        printf("Case %d:\n",cas);
        dfs(0,0);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值