Tempter of the Bone

来源:互联网 发布:教化妆的软件 编辑:程序博客网 时间:2024/05/21 14:06

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 

题意:给定N*M一张图,问你从起点S到终点D不经过障碍物X恰好K步能否到达?

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,m,t,sum,flag;char map[11][11];int vis[11][11];int sx,ex,sy,ey;int dx[]={-1,0,1,0};int dy[]={0,1,0,-1};void dfs(int x,int y,int step){    if(flag)        return;    if(x==ex&&y==ey&&step==t)    {        flag=1;        return;    }    for(int i=0;i<4;i++)    {        int xx=x+dx[i];        int yy=y+dy[i];        if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!='X'&&!vis[xx][yy])        {            vis[xx][yy]=1;            dfs(xx,yy,step+1);            vis[xx][yy]=0;        }    }}int main(){    while(~scanf("%d %d %d",&n,&m,&t),n||m||t)    {        sum=0;        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            getchar();            for(int j=0;j<m;j++)            {                scanf("%c",&map[i][j]);                if(map[i][j]=='S')                {                    sx=i;                    sy=j;                }                if(map[i][j]=='D')                {                    ex=i;                    ey=j;                }                if(map[i][j]=='X')                    sum++;            }        }        if(n*m-1-sum<t)        {            printf("NO\n");            continue;        }        flag=0;        vis[sx][sy]=1;        dfs(sx,sy,0);        if(flag)            printf("YES\n");        else            printf("NO\n");    }    return 0;}


减枝优化的:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int flag,sx,sy,ex,ey,num;int n,m,t,vis[10][10];int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};char map[10][10];void dfs(int x,int y,int sum){    int i,xx,yy;    if(flag==1)        return;    if(x==ex&&y==ey&&sum==t)    {        flag=1;        return;    }    int mindis=abs(x-ex)+abs(y-ey);    if(mindis>t-sum)//优化        return;    for(i=0;i<4;i++)    {        xx=x+dx[i];        yy=y+dy[i];        if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X')        {            vis[xx][yy]=1;            dfs(xx,yy,sum+1);            vis[xx][yy]=0;        }    }}int main(){    int i,j;    while(~scanf("%d%d%d",&n,&m,&t))    {        if(n==0&&m==0&&t==0)            break;        num=0;        for(i=0;i<n;i++)        {            scanf("%s",map[i]);            for(j=0;j<m;j++)            {                if(map[i][j]=='S')                {                    sx=i;                    sy=j;                }                if(map[i][j]=='D')                {                    ex=i;                    ey=j;                }                if(map[i][j]=='X')                    num++;            }        }        if(n*m-num-1<t)        {            printf("NO\n");            continue;        }        flag = 0;        memset(vis, 0, sizeof(vis));        vis[sx][sy] = 1;           dfs(sx, sy, 0);        if(flag)           printf("YES\n");        else           printf("NO\n");    }    return 0;}

原创粉丝点击