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