Tempter of the Bone

本文介绍了一种迷宫逃脱问题的解决方法,采用深度优先搜索(DFS)算法帮助小狗在限定时间内找到出口。通过分析迷宫布局和计算可行路径,确保小狗能在门开启的瞬间到达。文章提供了详细的代码实现。

Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 3
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 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES

Author

ZHANG, Zheng

Source

ZJCPC2004
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int Len_X,Len_Y,D,SIGN;
 4 int End_x,End_y;
 5 char Map[100][100];
 6 int DFS(int x,int y,int d,int D)
 7 {
 8     if(SIGN==1)return 1;
 9     if(x==End_x&&y==End_y&&d==D){SIGN+=1;return 1;}
10     if(Map[x][y]=='X')return 0;
11     else Map[x][y]='X';
12     if(d>=D) {return 0;}
13     if(x+1<Len_X&&Map[x+1][y]!='X')
14     {
15         DFS(x+1,y,d+1,D);
16         Map[x+1][y]='.';
17     }
18     if(x-1>=0&&Map[x-1][y]!='X')
19     {
20         DFS(x-1,y,d+1,D);
21         Map[x-1][y]='.';
22     }
23     if(y+1<Len_Y&&Map[x][y+1]!='X')
24     {
25         DFS(x,y+1,d+1,D);
26         Map[x][y+1]='.';
27     }
28     if(y-1>=0&&Map[x][y-1]!='X')
29     {
30         DFS(x,y-1,d+1,D);
31         Map[x][y-1]='.';
32     }
33     return 0;
34 }
35 
36 int main()
37 {
38     int Begin_x,Begin_y,i,j,flat,A,B;
39     while(scanf("%d%d%d",&Len_X,&Len_Y,&D)!=EOF)
40     {
41         if(Len_X==0&&Len_Y==0&&D==0)return 0;
42         getchar();
43         for(i=0;i<Len_X;i++)
44         {
45             for(j=0;j<Len_Y;j++)
46             {
47                 scanf("%c",&Map[i][j]);
48                 if(Map[i][j]=='S')
49                 {Begin_x=i;Begin_y=j;}
50                 if(Map[i][j]=='D')
51                 {End_x=i;End_y=j;}
52             }
53             getchar();
54         }
55         SIGN=0;
56         A=(End_y-Begin_y)>0?(End_y-Begin_y):-(End_y-Begin_y);
57         B=(End_x-Begin_x)>0?(End_x-Begin_x):-(End_x-Begin_x);
58         if(D%2==(A+B)%2)
59             DFS(Begin_x,Begin_y,0,D);
60         if(SIGN!=0)
61             printf("YES\n");
62         else
63             printf("NO\n");
64     }
65     return 0;
66 }
View Code

 修改:2015.5.8

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 using namespace std;
 5 int Len_X,Len_Y,D,SIGN;
 6 int End_x,End_y;
 7 char Map[100][100];
 8 void DFS(int x,int y,int d)
 9 {
10     //printf("%d %d %d\n",x,y,D);getchar();
11     if(SIGN==1)return ;
12     if(x==End_x&&y==End_y&&d==D){SIGN=1;return ;}
13     if(d>=D) return ;
14     if(Map[x][y]=='X')return ;
15     else
16     {
17         Map[x][y]='X';
18         DFS(x+1,y,d+1);
19         DFS(x-1,y,d+1);
20         DFS(x,y+1,d+1);
21         DFS(x,y-1,d+1);
22         Map[x][y]='.';
23     }
24    
25     return ;
26 }
27 
28 int main()
29 {
30     int Begin_x,Begin_y,i,j,A,B;
31     while(scanf("%d%d%d",&Len_X,&Len_Y,&D)!=EOF)
32     {
33         if(Len_X==0&&Len_Y==0&&D==0)return 0;
34         Begin_x=-1;Begin_y=-1;End_x=-2;End_y=-2;
35         for(i=0;i<=Len_X+1;i++)
36         {
37             for(j=0;j<=Len_Y+1;j++)
38             {
39                 if(i==0||j==0||i==Len_X+1||j==Len_Y+1)Map[i][j]='X';
40                 else
41                 {
42                     scanf(" %c",&Map[i][j]);
43                     if(Map[i][j]=='S')
44                     {Begin_x=i;Begin_y=j;Map[i][j]='.';}
45                     if(Map[i][j]=='D')
46                     {End_x=i;End_y=j;Map[i][j]='.';}
47                 }
48 
49             }
50         }
51         SIGN=0;
52         A=(End_y-Begin_y)>0?(End_y-Begin_y):-(End_y-Begin_y);
53         B=(End_x-Begin_x)>0?(End_x-Begin_x):-(End_x-Begin_x);
54         if(D%2==(A+B)%2)
55         {
56             DFS(Begin_x,Begin_y,0);
57            /* DFS(Begin_x+1,Begin_y,1);
58             DFS(Begin_x-1,Begin_y,1);
59             DFS(Begin_x,Begin_y+1,1);
60             DFS(Begin_x,Begin_y-1,1);*/
61         }
62         if(SIGN)
63             printf("YES\n");
64         else
65             printf("NO\n");
66     }
67     return 0;
68 }
View Code

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #define ABS(a,b)(((a)>=(b))?((a)-(b)):((b)-(a)))
 5 using namespace std;
 6 int Len_X,Len_Y,D,SIGN;
 7 int End_x,End_y;
 8 char Map[100][100];
 9 void DFS(int x,int y,int d)
10 {
11     //printf("%d %d %d\n",x,y,D);getchar();
12     if(SIGN==1)return ;
13     if(x==End_x&&y==End_y&&d==D){SIGN=1;return ;}
14     if(d>=D) return ;
15     if(Map[x][y]=='X')return ;
16     else
17     {
18         Map[x][y]='X';
19         DFS(x+1,y,d+1);
20         DFS(x-1,y,d+1);
21         DFS(x,y+1,d+1);
22         DFS(x,y-1,d+1);
23         Map[x][y]='.';
24     }
25    
26     return ;
27 }
28 
29 int main()
30 {
31     int Begin_x,Begin_y,i,j,A,B;
32     while(scanf("%d%d%d",&Len_X,&Len_Y,&D)!=EOF)
33     {
34         if(Len_X==0&&Len_Y==0&&D==0)return 0;
35         Begin_x=-1;Begin_y=-1;End_x=-2;End_y=-2;
36         for(i=0;i<=Len_X+1;i++)
37         {
38             for(j=0;j<=Len_Y+1;j++)
39             {
40                 if(i==0||j==0||i==Len_X+1||j==Len_Y+1)Map[i][j]='X';
41                 else
42                 {
43                     scanf(" %c",&Map[i][j]);
44                     if(Map[i][j]=='S')
45                     {Begin_x=i;Begin_y=j;Map[i][j]='X';}/*标记已经走过*/
46                     if(Map[i][j]=='D')
47                     {End_x=i;End_y=j;}
48                 }
49 
50             }
51         }
52         SIGN=0;
53         A=ABS(End_x,Begin_x);
54         B=ABS(End_y,Begin_y);
55         if(D%2==(A+B)%2)
56         {
57             //DFS(Begin_x,Begin_y,0);
58             DFS(Begin_x+1,Begin_y,1);
59             DFS(Begin_x-1,Begin_y,1);
60             DFS(Begin_x,Begin_y+1,1);
61             DFS(Begin_x,Begin_y-1,1);
62         }
63         if(SIGN)
64             printf("YES\n");
65         else
66             printf("NO\n");
67     }
68     return 0;
69 }
View Code


 

转载于:https://www.cnblogs.com/Wurq/p/3750332.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值