HDOJ1010 Tempter of the Bone(DFS+奇偶剪枝)

来源:互联网 发布:ubuntu 12.04 163源 编辑:程序博客网 时间:2024/06/15 20:27

Tempter of the Bone

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


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
 

Author
ZHANG, Zheng
 

Source
ZJCPC2004


【分析】题意:给定N*M迷宫(‘S’-起点,’D’-终点,‘X’-墙,‘.‘-空地)及时刻T,门只有T时刻才开。问小狗能否恰好在T时刻到达终点?注意空地只能走一次。

        不难发现这是一个DFS问题,当然使用BFS亦可。不过朴素解法会超时。

#include <stdio.h>#include <string.h>int N,M,T;int suc;int sx,sy,ex,ey;char maze[10][10];int visited[10][10];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};void dfs(int x,int y,int step){    int i,xx,yy;    if(step>T)        return;    if(suc==1)    return;    if(x==ex && y==ey && step==T)    {        suc=1;        return;    }    for(i=0;i<4;i++)    {        xx=x+dir[i][0];        yy=y+dir[i][1];        if(xx>=0 && xx<N && yy>=0 && yy<M && !visited[xx][yy] && maze[xx][yy]!='X')        {            visited[xx][yy]=1;            dfs(xx,yy,step+1);visited[xx][yy]=0;        }    }}int main(){    int i,j;    while(scanf("%d %d %d",&N,&M,&T) && N && M && T)    {        for(i=0;i<N;i++)        {            scanf("%s",maze[i]);            for(j=0;j<M;j++)            {                if(maze[i][j]=='S')                {                    sx=i;                    sy=j;                }                else if(maze[i][j]=='D')                {                    ex=i;                    ey=j;                }            }        }        suc=0;        memset(visited,0,sizeof(visited));        dfs(sx,sy,0);        if(suc==1)            printf("YES\n");        else            printf("NO\n");    }    return 0;}

        因此要考虑剪枝优化提高效率。

        这里使用的剪枝方法是奇偶剪枝 https://baike.baidu.com/item/%E5%A5%87%E5%81%B6%E5%89%AA%E6%9E%9D/10385689?fr=aladdin

        记起点S到终点D的最短距离(这里即“曼哈顿距离”)为minstep,若①minstep>T 或 ②T-minstep为奇数,则可以直接判定无解,输出"NO"。

#include <stdio.h>#include <math.h>#include <string.h>int N,M,T;int suc;int sx,sy,ex,ey;char maze[10][10];int visited[10][10];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};void dfs(int x,int y,int step){    int i,xx,yy;    if(step>T)        return;    if(suc==1)    return;    if(x==ex && y==ey && step==T)    {        suc=1;        return;    }    for(i=0;i<4;i++)    {        xx=x+dir[i][0];        yy=y+dir[i][1];        if(xx>=0 && xx<N && yy>=0 && yy<M && !visited[xx][yy] && maze[xx][yy]!='X')        {            visited[xx][yy]=1;            dfs(xx,yy,step+1);visited[xx][yy]=0;        }    }}int main(){    int i,j;    int minstep;    while(scanf("%d %d %d",&N,&M,&T) && N && M && T)    {        for(i=0;i<N;i++)        {            scanf("%s",maze[i]);            for(j=0;j<M;j++)            {                if(maze[i][j]=='S')                {                    sx=i;                    sy=j;                }                else if(maze[i][j]=='D')                {                    ex=i;                    ey=j;                }            }        }        minstep=abs(ex-sx)+abs(ey-sy);      //奇偶剪枝         if((minstep>T) || (((T-minstep)&1)!=0))printf("NO\n");else{    suc=0;    memset(visited,0,sizeof(visited));    visited[sx][sy]=1;    dfs(sx,sy,0);    if(suc==1)        printf("YES\n");    else        printf("NO\n");}}    return 0;}


阅读全文
0 0
原创粉丝点击