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. 
InputThe 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. 
OutputFor each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise. 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES

N 行 M 列 T 时间,看这个doggie是否能在第T秒刚好到达门。S是doggie的起始位置,D是门的位置。

一开始我对这个时间T有点不理解,拿题目中例2来说S走4步在D的前一个位置,再走一步到达D,题目是要在5秒刚好在D,而不是所谓的在时间T内到达就行了。

这道题里涉及一些剪枝问题,如果不剪枝就会超时。(我们来把这道题的迷宫想象成一块一块的格子)。

①n*m-wall<=t . n*m代表总共的格子,n*m-wall代表能走的格子数目,t是时间,n*m-wall<=t也就是可以走的格子走完了时间T都没有到,肯定不能到达D。至于为什么有=,看的一个大佬写的一个例子:

3 3 4

S X X 

. X X

X . D     在这里n*m-wall=3*3-5=4=t,这里是不能到达的。

②奇偶剪枝

因为两点之间直线最短,所以由S到达D的最短路径就是abs(sx-dx)+abs(sy-dy),可以把t理解为题目要你走的总步数,t-step就代表你任意走的总步数,因为abs(sx-dx)+abs(sy-dy)已经是最短的步数了,你走的一定不可能比它更短,所以t-step肯定是要>=最短步数的。所以当t-step<abs(sx-dx)+abs(sy-dy)的时候,我们就给它剪出去。(注意sx,sy,step都是在变化的

还有t-step-abs(sx-dx)-abs(sy-dy)必须是偶数,如果是奇数就不能到达D,给个例子大家看看吧 

3行4列    最短距离是5。红色箭头代表随意走的路线,一共9步,9-5=4为偶数,刚好到达D,如果为奇数,那最后一步到达的肯定是D的周围而不能刚好到达D。

S 
→ D
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
char maze[7][7];
int direct[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n,m,t,dx,dy;
bool flag;
void dfs(int x,int y,int step)
{
    int i,p,x1,y1;
    if(x<=0||x>n||y<=0||y>m)
        return;
    if(x==dx&&y==dy&&step==t)
    {
        flag=1;
        return;
    }
    p=(t-step)-abs(x-dx)-abs(y-dy);
    if(p<0||p&1)
        return ;
    for(i=0;i<4;i++)
    {
        x1=x+direct[i][0];
        y1=y+direct[i][1];
        if(maze[x1][y1]!='X')
        {
            maze[x1][y1]='X';
            dfs(x1,y1,step+1);
            if(flag)
                return ;
            maze[x1][y1]='.';
        }
    }
    return;
}
int main()
{
    int i,j,sx,sy,wall;
    while(scanf("%d%d%d",&n,&m,&t)&&(m+n+t))
    {
       wall=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>maze[i][j];
                if(maze[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                }
                else if(maze[i][j]=='D')
                {
                    dx=i;
                    dy=j;
                }
                else if(maze[i][j]=='X')
                    wall++;
            }
        }
        flag=0;
        maze[sx][sy]='X';
        dfs(sx,sy,0);
        if(n*m-wall<=t)
        {
            printf("NO\n");
            continue;
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值