Tempter of the Bone HDU 1010

Tempter of the Bone

点击打开1010

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 40   Accepted Submission(s) : 12
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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

ZJCPC2004

Statistic | Submit | Back

题目大意:就是S是起点,X是墙,D是终点,看在T时间内是否能到达D点,如果能到达,则输出YES,否则输出NO。
DFS解决每次向四个方向搜索,如果到达D时,正好使得花费的时间等于T,则输出YES,否则则不能找到输出NO。
注意:题目给的map地图看起来并不大,但是由于用的DFS递归实现的,所以比较慢,如果不惊醒剪枝的话,超时就是个问题了。
下面通过百科(转自百度百科)介绍一下奇偶剪枝:
奇偶剪枝是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
s
    
|
    
|
    
|
    
+
e
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
s
 
 
+
 
|
+
   
|
    
+
e
如图,为一般情况下非 最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);

2结论编辑

推广之,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
代码:
#include <iostream>
#include<cstdio>
#include<cmath>
#include<memory.h>
using namespace std;
char map[10][10];
int sloved,n,m,t;
struct x_y
{
    int x;
    int y;
} lc,dlc;
int distance1(x_y star,x_y destion)//算最短距离
{
    return fabs(star.x-destion.x)+fabs(star.y-destion.y);
}
void dfs(x_y star,int cnt)
{
    int temp;
    if(sloved) return;
    if(star.x==dlc.x&&star.y==dlc.y&&cnt==t)//满足题目要求则令sloved=1;
    {
        sloved=1;
    }
    if(star.x==lc.x&&star.y==lc.y&&cnt!=0)//如果又搜索到起点则回溯
    {
        return ;
    }
    if(star.x<0||star.y<0||star.x>=n||star.y>=m)//判断是否超出边界,如果超出则回溯
        return ;
    temp=distance1(star,dlc);
    if ( cnt >= t ||map[star.x][star.y]=='D') return;
    int dis = t - cnt - temp;
    if ( dis < 0 || dis % 2 ) return;//奇偶剪枝
    else
    {
        if((star.x+1)>=0&&(star.x+1)<n&&map[star.x+1][star.y]!='X')//从此点向下一行搜索
        {
            cnt++;
            star.x+=1;
            map[star.x][star.y]='X';
            dfs(star,cnt);
            map[star.x][star.y]='.';
            if(sloved)
                return ;搜索成功则返回
            else//不成功则回溯
            {
                cnt--;
                star.x-=1;
            }
        }
        if(((star.x-1)>=0)&&((star.x-1)<n)&&map[star.x-1][star.y]!='X')//向上一行搜索
        {
            cnt++;
            star.x-=1;
            map[star.x][star.y]='X';
            dfs(star,cnt);
            map[star.x][star.y]='.';
            if(sloved)//成功则返回
                return ;
            else//不成功则回溯
            {
                cnt--;
                star.x+=1;
            }
        }
        if(((star.y+1)>=0)&&((star.y+1)<=m)&&map[star.x][star.y+1]!='X')//向右一列搜索
        {
            star.y+=1;
            cnt++;
            map[star.x][star.y]='X';
            dfs(star,cnt);
            map[star.x][star.y]='.';
            if(sloved)
                return ;//成功返回
            else//不成功回溯
            {
                cnt--;
                star.y-=1;
            }
        }
        if(((star.y-1)>=0)&&((star.y-1)<=m)&&map[star.x][star.y-1]!='X')//&&map[star.x][star.y-1]!='S')//向左一列搜索
        {
            cnt++;
            star.y-=1;
             map[star.x][star.y]='X';
            dfs(star,cnt);
            map[star.x][star.y]='.';
            return ;四个方向都搜索完了返回
        }
    }
}
int s[10][10];
int main()
{
    int i,cnt,temp,j;
    for(i=0; i<10; i++)
        for(j=0; j<10; j++)
            s[i][j]=(i+j)%2;
    //  freopen("\\input1.txt","r",stdin);
    // freopen("\\output4444.txt","w",stdout);
    while(cin>>n>>m>>t&&(m||n||t))
    {
        sloved=0;
        cnt=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')//记录起点的位置
                {
                    lc.x=i;
                    lc.y=j;
                }
                if(map[i][j]=='D')记录终点的位置
                {
                    dlc.x=i;
                    dlc.y=j;
                }
                if(map[i][j]=='.')//记录可以走的步数(为了剪枝)
                    cnt++;
            }
        }
        //printf("lc.x %d lc.y %d\n",lc.x,lc.y);
        temp=distance1(lc,dlc);
        //printf("s[lc.x][lc.y]%d s[dlc.x][dlc.y]%d\n",s[lc.x][lc.y],s[dlc.x][dlc.y]);
        if(temp>t||(cnt+1<t)||(s[lc.x][lc.y]==s[dlc.x][dlc.y]&&t%2==1)||(s[lc.x][lc.y]!=s[dlc.x][dlc.y]&&t%2==0))//奇偶剪枝加路径剪枝
            cout<<"NO"<<endl;
        else
        {
            cnt=0;
            dfs(lc,cnt);
            //printf("lc.x %d lc.y %d\n",lc.x,lc.y);
            if(sloved)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
        memset(map,0,sizeof(map[0])*10);
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

PS:在上面的DFS函数中在向四个方向搜索的时候可以用一个循环加一个方向数组来实现:例如:
int d[][]={{1,0},{0,1},{-1,0},{0,-1}};
然后再写个循环,让他依次向四个方向搜索。
  for ( int i=0;i<4;i++)
     {
         if (s[x+dir[i][0]][y+dir[i][1]]== 'D' &&num+1==t)
             return 1;
         if (s[x+dir[i][0]][y+dir[i][1]]== '.' )
         {   
             s[x+dir[i][0]][y+dir[i][1]]= 'X' ;
             if (DFS(x+dir[i][0],y+dir[i][1],num+1))
                 return 1;
             s[x+dir[i][0]][y+dir[i][1]]= '.' ;
         }
     }
(代码摘于网络)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值