Tempter of the Bone——DFS(王道)

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

题目大意:还是迷宫的题目,S是起点,D是终点,X是墙,每秒一个位置,当好T秒走到

该题不再要求最优解,而是判定符合条件的路径,因此用深度优先搜索。

里面有关于剪枝的运用(因为如果不剪枝可能会超时),在54行,即判断起始点和时间的关系。

 1 #include <iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 char maze[8][8];//保存地图信息
 5 int n,m,t;//地图大小n*m,起点到终点能否恰好为t
 6 bool success;//是否找到所需状态标记
 7 int go[][2]={//s四个方向行走坐标差
 8     -1,0,
 9     1,0,
10     0,1,
11     0,-1
12 };
13
14 void DFS(int x,int y,int time){
15     for(int i=0;i<4;i++){
16         int nx=x+go[i][0];//枚举四个相邻位置
17         int ny=y+go[0][i];
18         if(nx<1 || nx>n || ny<1 || ny>m)//地图外
19             continue;
20         if(maze[nx][ny]==‘X‘)//碰墙
21             continue;
22         if(maze[nx][ny]==‘D‘){//到终点
23         if(time+1==t){//所用时间恰好为t
24             success=true;//搜索成功
25             return;
26         }
27         else
28             continue;
29     }
30     maze[nx][ny]=‘X‘;//该点设为墙
31     DFS(nx,ny,time+1);//递归扩展该状态
32     maze[nx][ny]=‘.‘;//把原来的路改回来
33     if(success)
34         return;
35     }
36 }
37
38 int main(){
39     while(scanf("%d %d %d",&n,&m,&t)!=EOF){
40         if(n==0 && m==0 && t==0)
41             break;
42         for(int i=1;i<=n;i++)
43             scanf("%s",maze[i]+1);
44         success=false;
45         int sx,sy;
46         for(int i=1;i<=n;i++){//寻找D的坐标
47             for(int j=1;j<=m;j++){
48                 if(maze[i][j]==‘D‘){
49                     sx=i;
50                     sy=j;
51                 }
52             }
53         }
54         for(int i=1;i<=n;i++){//找到S后,判断S和D的奇偶关系是否和t相符
55             for(int j=1;j<=m;j++){
56                 if(maze[i][j]==‘S‘ && (i+j)%2==((sx+sy)%2+t%2)%2){
57                     maze[i][j]=‘X‘;//起始点设为墙
58                     DFS(i,j,0);//递归扩展初始状态
59                 }
60             }
61         }
62         puts(success==true?"YES":"NO");
63     }
64     return 0;
65 }

//说实话我真的不是很懂43行,为什么要+1。。。请在评论区告诉我,多谢!

原文地址:https://www.cnblogs.com/xym4869/p/8625608.html

时间: 2024-08-03 18:20:24

Tempter of the Bone——DFS(王道)的相关文章

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

Tempter of the Bone(dfs奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 92175    Accepted Submission(s): 25051 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(DFS剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 129289    Accepted Submission(s): 34906 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 82702    Accepted Submission(s): 22531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1010 Tempter of the Bone DFS+奇偶剪枝,入门题

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 78390    Accepted Submission(s): 21395 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu1010 Tempter of the Bone(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 90716    Accepted Submission(s): 24683 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

B - Tempter of the Bone(DFS+剪枝)

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

HDU 1010 Tempter of the Bone DFS 简单题 注意剪枝

题意:一只小狗要刚好在t时刻从起点都到终点,问可不可以. 注意剪枝. 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 using namespace std; 5 int maze[9][9]; 6 bool vis[9][9]; 7 int n,m,t; 8 bool ans; 9 struct Point 10 { 11 int x,y; 12 }; 13 int dx[4]={0,0,-1,1}