HDU---1010-Tempter of the Bone

来源:互联网 发布:linux运维岗笔试题 编辑:程序博客网 时间:2024/06/06 18:08

Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 2

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

Sample Output

NOYES

Author

ZHANG, Zheng

Source

ZJCPC2004



AC CODE:

#include <iostream>#include <cmath>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;char mmap[9][9];int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};int start_x,start_y,end_x,end_y;int x,y,n,poi=0;bool isplace(int xx,int yy){    if(xx<=x && yy<=y && xx>0 && yy>0)        if(mmap[xx][yy]!='X')        return true;    return false;}void DFS(int a,int b,int temp){    if(temp==n && a==end_x && b==end_y)    {        poi=1;        return;    }    if(poi)return;    int t=abs(a-end_x)+abs(b-end_y)-abs(temp-n);// 优化:奇偶判断    if(t>0||t&1)return;    int k,xx,yy;    for(k=0; k<=3; ++k)    {         xx=a+dir[k][0];         yy=b+dir[k][1];         if(isplace(xx,yy))         {             mmap[xx][yy]='X';             DFS(xx,yy,temp+1);             mmap[xx][yy]='.';         }    }}int main(){    int i,j;    while(scanf("%d%d%d",&x,&y,&n)!=EOF&&(x||y||n))    {        if(x==0)break;        for(i=1; i<=x; ++i)        {            for(j=1; j<=y; ++j)            {                cin>>mmap[i][j];                if(mmap[i][j]=='S')                {                    start_x=i;                    start_y=j;                }                if(mmap[i][j]=='D')                {                    end_x=i;                    end_y=j;                }            }        }        poi=0;        mmap[start_x][start_y]='X';        DFS(start_x,start_y,0);        if(poi)cout<<"YES\n";        else cout<<"NO\n";    }    return 0;}


0 0
原创粉丝点击