Tempter of the Bone

来源:互联网 发布:淘宝网折八百 编辑:程序博客网 时间:2024/04/30 00:25

题目:



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
题意:

从S走到D 只有.可以走,X不能走通,但必须要经过给定的时间,走一个点1s,若从S到D正好可以是t秒的话就YES

思路:

搜索,变形的走迷宫,在每一个点走他的上下左右,刚开始写的总是错,后来已被递归晕了,还是决定换种方法重写一次。

代码:

#include<stdio.h>#include<string.h>int n,m,t,f,mm[51][51],ok;char str[51][51];void dfs(int a,int b,int p){    mm[a][b]=1;    if (p>t||ok==1)return;//刚开始少了这句就超时了。。。如果时间已超过t,或之前的已经可以了,就return;    if (str[a][b]=='D')    {        if (p==t){            ok=1;            return ;        }    }    else    {        if (str[a][b]=='.'||str[a][b]=='S'){                 if (!mm[a-1][b]&&a-1>=0) {dfs(a-1,b,p+1),mm[a-1][b]=0;}//这个重新赋为0,就说明他的下一个已走不通了,还得退回到他开始走。递归函数时要把p+1传回去,这样才可以记住到这一步的时候已经耗了多长时间。                 if (!mm[a][b+1]&&b+1<m) {dfs(a,b+1,p+1),mm[a][b+1]=0;}                 if (!mm[a][b-1]&&b-1>=0) {dfs(a,b-1,p+1),mm[a][b-1]=0;}                 if (!mm[a+1][b]&&a+1<n) {dfs(a+1,b,p+1),mm[a+1][b]=0;}        }    }}int main(){    while (scanf("%d%d%d",&n,&m,&t)!=EOF){        if (n==0&&m==0&&t==0)            break;            f=0;            ok=0;            int i,j;            memset(mm,0,sizeof (mm));        for (i=0;i<n;i++)            scanf("%s",str[i]);           // printf("1");        for (i=0;i<n;i++){        for (j=0;j<m;j++){            if (str[i][j]=='S'){                   // printf("1");                dfs(i,j,0);                break;            }        }        }        if (ok)            printf("YES\n");        else            printf("NO\n");    }    return 0;}


0 0
原创粉丝点击