深搜+剪枝Tempter of the Bone

题目描述:

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

input:
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door; or
‘.’: an empty block.

The input is terminated with three 0’s. This test case is not to be processed.
output:
For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.

意思:

有一个N* M的迷宫,里面有一个门,在第T秒是会自动打开一小会时间(小于1)秒,一只狗需要在第T秒恰巧到达门,才能走出迷宫,问能否走出?(狗不能再一个地方超过1秒钟,并且不能回到已经走过的地方)

‘X’:一堵狗狗不能进入的墙;
‘S’:小狗的起点;
’ D ':门;
’ . ’ :一个空街区。
能输出 “YES",否则输出 ”NO";

题目分析

因为是恰好在T时间的时候到达门,而不是求最短距离,则用深搜的方法,但是直接深搜会超时,这就需要必要的剪枝。
剪枝:用ans表示出目前狗与门的相对距离,即理想情况下用的最短时间
t表示目前所剩的时间

  • 如果t<ans,则不会到达输出NO(即 即使最理想的情况下也无法到达,时间不够)//这是最普遍用的剪枝,但是只有这一个还是过不了
  • (t-ans)如果是奇数则不会在T时刻恰巧到达输出NO(因为需要T时刻恰巧到达,如果相对理想情况下要绕路,则绕路所走的步数一定是偶数)

下面的代码没没剪枝,但是过了(别人的没过QWQ)

//没写剪枝,但是过了~~qwq~~ 
#include <iostream>
#include<cstring>
using namespace std;
int m,n;
char mapn[10][10];//地图
int vis[10][10];//标记
int flag;
int t;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dis(int i,int j,int anst)
{
    if((anst>t)||(mapn[i][j]=='D'&&anst!=t))
        return;
    if(mapn[i][j]=='D'&&anst==t)
    {
        flag=1;
        cout<<"YES"<<endl;
        return;
    }
    int x,y;
    vis[i][j]=1;
    for(int t=0;t<4;t++){
        x=i+dx[t];
        y=j+dy[t];
        if(x>=0&&x<n&&y<m&&y>=0&&!vis[x][y]&&mapn[x][y]!='X'){
            dis(x,y,anst+1);
        }
        if(flag)
            break;
    }
    vis[i][j]=0;
    return;
}
int main()
{
    int start1,start2;
    while(1)
    {
        flag=0;
        cin>>n>>m>>t;
        cin.ignore();
        //判断退出
        if(n==0&&m==0)
            break;
        //输入
        for(int i=0;i<n;i++)
        {
            cin>>mapn[i];
            for(int j=0;j<m;j++)
            {
                if(mapn[i][j]=='S')
                    start1=i,start2=j;
            }
        }

        //深搜
        dis(start1,start2,0);
        if(!flag)
            cout<<"NO"<<endl;
        memset(vis,0,sizeof(vis));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值