HDU 1010 Tempter of the Bone(DFS,奇偶剪枝)

153 篇文章 0 订阅
29 篇文章 0 订阅

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 101688    Accepted Submission(s): 27539


Problem Description
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.
 

Sample Input
  
  
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 

Sample Output
  
  
NO YES
 

Author
ZHANG, Zheng
 

Source


题意:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,
问:在指定的时间,是否能到达'门'的位置.
注意:路不可以重复经过,时间也要刚好是 t ,不能少.
思路:还是DFS,不能用BFS,因为BFS求的是最短路径,而此题的路径不一定最短.
剪枝是关键,奇偶剪枝.
奇偶剪枝原理:
要理解奇偶剪枝,先了解一下曼哈顿距离,从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,
路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数.
本题还有一个剪枝:n*m-wall与t的关系,wall为'X'的数量,解释一下,n*m为区域总数,
所以m*n-wall<=t 一定不到到达终点,注意,少时等号在杭电上运行时间为546MS,而加上等号运行时间才为78MS!
eg: 
3 3 4
SXX
.XX
X.D
上面的例子满足m*n-wall=t,确实不能到达,但不能找到合理的解释......


AC代码1:(用'X'标记走过的地方)

#include <iostream>
#include <cstring>
using namespace std;
char a[10][10];
int n,m,t;
int sx,sy,ex,ey;
bool flag;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int abs(int x)
{
    return x<0?-x:x;
}
void DFS(int x,int y,int time)//当前位置坐标(x,y),到目前位置消耗时间
{
    if(x<0||x>=n||y<0||y>=m)
        return;
    if(x==ex&&y==ey&&time==t)
    {
        flag =true;
        return ;
    }
    if(flag)
        return;
    int temp=(t-time)-(abs(x-ex)+abs(y-ey));
    if(temp<0||temp&1)
        return;
    for(int i=0;i<4;i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(a[xx][yy]!='X')
        {
            a[xx][yy]='X';
            DFS(xx,yy,time+1);
            a[xx][yy]='.';
            if(flag)
                return ;
        }
    }
}
int main()
{
    while(cin>>n>>m>>t)
    {
        if(n==0&&m==0)
            break;
        int wall=0;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='S')
                    sx=i,sy=j;
                else if(a[i][j]=='D')
                    ex=i,ey=j;
                else if(a[i][j]=='X')
                    wall++;
            }
        if(n*m-wall<=t)
        {
            cout<<"NO"<<endl;
            continue;
        }
        flag=false;
        a[sx][sy]='X';
        DFS(sx,sy,0);
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

AC代码2:(用vis[][])

#include <iostream>
#include <cstring>
using namespace std;
int n,m,t;
int ex,ey;
bool flag;
char a[10][10];
bool vis[10][10];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
bool OK(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return false;
    return true;
}
int abs(int x)
{
    return x<0?-x:x;
}
void DFS(int x,int y,int tx)
{
    //cout<<"x="<<x<<",y="<<y<<",tx="<<tx<<endl;
    if(x==ex&&y==ey&&tx==t)
    {
        flag=true;
        return;
    }
    if(flag) return ;
    int tt=t-tx-(abs(x-ex)+abs(y-ey));
    if(tt<0||tt&1)
        return;
    for(int i=0; i<4; i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(OK(xx,yy)&&!vis[xx][yy]&&a[xx][yy]!='X')
        {
            vis[xx][yy]=true;
            DFS(xx,yy,tx+1);
            vis[xx][yy]=false;
        }
    }
}
int main()
{
    while(cin>>n>>m>>t,n,m,t)
    {
        for(int i=0; i<n; i++)
            cin>>a[i];
        int x=0;
        int sx,sy;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(a[i][j]=='S')
                    sx=i,sy=j;
                else if(a[i][j]=='X')
                    x++;
                else if(a[i][j]=='D')
                    ex=i,ey=j;
            }
        }
        if(m*n-x<=t)
        {
            cout<<"NO"<<endl;
            continue;
        }
        memset(vis,false,sizeof(vis));
        vis[sx][sy]=true;
        flag=false;
        DFS(sx,sy,0);
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}



  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值