HDU 1010 Tempter of the bond

来源:互联网 发布:linux 文件后面带星号 编辑:程序博客网 时间:2024/04/30 02:00
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
#include <iostream>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;int m,n,k;int sx,sy,di,dj;char a[10][10];int dir[4][2]= {1,0,-1,0,0,1,0,-1};int dfs(int num,int sx,int sy){    if((num==k)&&(sx==di)&&(sy==dj))        return 1;    if(num>=k)        return 0;    //if(abs(di-sx)+abs(dj-sy)>k-num)    //return 0;    if((abs(di-sx)+abs(dj-sy))%2!=(k-num)%2)        return 0;    int xx,yy;    for(int i=0; i<4; i++)    {        xx=sx+dir[i][0];        yy=sy+dir[i][1];        if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!='X')        {            a[xx][yy]='X';            if(dfs(num+1,xx,yy))                return 1;            a[xx][yy]='.';//回溯的精华;        }    }    return 0;}int main(){    while(cin>>n>>m>>k)    {        if(n==0&&m==0&&k==0)            break;        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;                    a[i][j]='X';                }                else if(a[i][j]=='D')                {                    di=i;                    dj=j;                }            }        }        int t=dfs(0,sx,sy);        if(t==0)            cout<<"NO"<<endl;        else            cout<<"YES"<<endl;    }    return 0;}

C语言
#include<stdio.h>#include<math.h>#include<stdlib.h>//abs的头文件;char s[10][10];int n,m,k;int sx,sy;int di,dj;//wall=0;/*struct tv{    int x;    int y;    int number;} T[10010];*/int da[4][2]= {1,0,0,-1,-1,0,0,1};//四个方向;int dfs(int num,int sx,int sy){    //tv a,a1;    int xx,yy;//g=0,h=0;    //a.x=sx;//起点坐标;    //a.y=sy;    //a.number=0;    //T[g]=a;//起点的坐标赋值给T[0];    //g++;//g=0;    if((num==k)&&(sx==di)&&(sy==dj))//时间相等而且正好到达终点;        return 1;    if(num>=k)        return 0;    if(abs(di-sx)+abs(dj-sy)>k-num)//在时间内从当前坐标到终点的最近距离也无法到达;        return 0;    if((k-num-abs(di-sx)-abs(dj-sy))%2!=0)//只有是偶数的时候才能到达;        return 0;    for(int i=0; i<4; i++)    {        xx=sx+da[i][0];//向左右移动        yy=sy+da[i][1];//向上下移动        if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]!='X')        {            s[xx][yy]='X';//走过的标记为X;            if(dfs(num+1,xx,yy))                return 1;            s[xx][yy]='.';//回溯的时候标记为.;        }    }    return 0;}int main(){    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        if(n==0&&m==0&&k==0)            break;        for(int i=0; i<n; i++)        {            scanf("%s",s[i]);//按行输入;            for(int j=0; j<m; j++)            {                if(s[i][j]=='S')                {                    sx=i;//确定起点的坐标;                    sy=j;                    s[i][j]='X';//走过之后标记为X;                }                else if (s[i][j]=='D')                {                    di=i;                    dj=j;//终点坐标;                }            }            getchar();        }        int t=dfs(0,sx,sy);//回溯;        if(t==0)            printf("NO\n");        else            printf("YES\n");    }}


原创粉丝点击