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.
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.
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
NO YES
分析:
s | ||||
| | ||||
| | ||||
| | ||||
+ | — | — | — | e |
s | — | — | — | |
— | — | + | ||
| | + | |||
| | ||||
+ | — | — | — | e |
解题思路:这道题,要用到剪枝搜索来做,否则会超时。剪掉的条件是,如果可走地板数目小于给定的时间,绝对不可能得救。还有就是狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门,也不可能得救,剪掉这两种情况后。就用DFS来做。从起点出发,DFS周围的路,走过的路就标记为不可走,一只搜索下去,如果是一条不归路(就是活不了啦)就回溯,把可能的路都搜索一遍过去,看看是否有可行方案。
源码:(TLE)——未剪枝、未加优化
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; int n,m,t; char map[10][10]; int flag; int di,dj,wall; int i,j,si,sj; int to[4][2] = {-1,0,1,0,0,-1,0,1}; void dfs(int si,int sj,int cnt)//深搜 { if(si>=n || sj>=m || si<0 || sj < 0)//出界 return ; if(cnt == t && si == di && sj == dj)//到达终点 flag = 1; if(flag) return ; for(int i = 0; i<4; i++) { int nextx=si+to[i][0]; int nexty=sj+to[i][1]; if(nextx>=0&&nextx<m&&nexty>=0&&nexty<n&&map[nextx][nexty]!='X') { map[nextx][nexty]='X';//走过的地方变为墙 dfs(nextx,nexty,cnt+1); map[nextx][nexty]='.';//迷宫还原,以便下次广搜 } } return ; } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0)) { getchar(); wall = 0; for(i = 0; i<n; i++) for(j = 0; j<m; j++) { cin>>map[i][j]; if(map[i][j] == 'S') { si = i; sj = j; //标记起点 } else if(map[i][j] == 'D') { di = i; dj = j; //标记终点 } else if(map[i][j] == 'X') wall++;//对“#计数” } flag = 0; map[si][sj] = 'X';//出发点是不可能再走的了,变为墙 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
剪枝优化后——Accept
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; int n,m,t; char map[10][10]; int flag; int di,dj,wall; int i,j,si,sj; int to[4][2] = {-1,0,1,0,0,-1,0,1}; void dfs(int si,int sj,int cnt) //深搜 { if(si>=n || sj>=m || si<0 || sj < 0) //出界 return ; if(cnt == t && si == di && sj == dj) //到达终点 flag = 1; if(flag) return ; for(int i = 0; i<4; i++) { int nextx=si+to[i][0]; int nexty=sj+to[i][1]; if(map[nextx][nexty]!='X') { map[nextx][nexty]='X';//走过的地方变为墙 dfs(nextx,nexty,cnt+1); map[nextx][nexty]='.';//迷宫还原,以便下次广搜 } } return ; } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0)) { getchar(); wall = 0; for(i = 0; i<n; i++) for(j = 0; j<m; j++) { cin>>map[i][j]; if(map[i][j] == 'S') { si = i; sj = j; //标记起点 } else if(map[i][j] == 'D') { di = i; dj = j; //标记终点 } else if(map[i][j] == 'X') wall++;//对“#计数” } if(n*m-wall<=t||abs(si-di)+abs(sj-dj)>t)//t是代表要走的步数,步数加墙数必须小于总格子数的,因为所有格子中还包括了S和D,这是剪枝 { printf("NO\n"); continue; //abs(si-ei)+abs(sj-ej)为起点到终点的最短步数 } if((abs(si-di)+abs(sj-dj))%2!=(t%2)) { //狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门 printf("NO\n"); continue; } flag = 0; map[si][sj] = 'X';//出发点是不可能再走的了,变为墙 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
以下代码也能通过,证明,此题主要是奇偶剪枝:
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; int n,m,t; char map[10][10]; int flag; int di,dj,wall; int i,j,si,sj; int to[4][2] = {-1,0,1,0,0,-1,0,1}; void dfs(int si,int sj,int cnt) //深搜 { if(si>=n || sj>=m || si<0 || sj < 0) //出界 return ; if(cnt == t && si == di && sj == dj) //到达终点 flag = 1; if(flag) return ; for(int i = 0; i<4; i++) { int nextx=si+to[i][0]; int nexty=sj+to[i][1]; if(map[nextx][nexty]!='X') { map[nextx][nexty]='X';//走过的地方变为墙 dfs(nextx,nexty,cnt+1); map[nextx][nexty]='.';//迷宫还原,以便下次广搜 } } return ; } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0)) { getchar(); wall = 0; for(i = 0; i<n; i++) for(j = 0; j<m; j++) { cin>>map[i][j]; if(map[i][j] == 'S') { si = i; sj = j; //标记起点 } else if(map[i][j] == 'D') { di = i; dj = j; //标记终点 } else if(map[i][j] == 'X') wall++;//对“#计数” } /* if(n*m-wall<=t||abs(si-di)+abs(sj-dj)>t)//t是代表要走的步数,步数加墙数必须小于总格子数的,因为所有格子中还包括了S和D,这是剪枝 { printf("NO\n"); continue; //abs(si-ei)+abs(sj-ej)为起点到终点的最短步数 }*/ if((abs(si-di)+abs(sj-dj))%2!=(t%2)) { //狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门 printf("NO\n"); continue; } flag = 0; map[si][sj] = 'X';//出发点是不可能再走的了,变为墙 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }