Description
The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze
layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one
square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.
Sample Input
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
#include<queue> #include<stack> #include<math.h> #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; int t,n,m; int sx,sy,ex,ey,ans; int b[100][100]; char map[100][100]; int l[4][2]= {0,1,1,0,0,-1,-1,0}; int r[4][2]= {0,-1,1,0,0,1,-1,0}; int dfs1(int x,int y,int step) { if(x==ex&&y==ey) return step+1; if(x<0||x>=m||y<0||y>=n||map[x][y]=='#') return 0; ans=(ans+3)%4; int temp=0; while(1) { temp=dfs1(x+l[ans][0],y+l[ans][1],step+1); if(temp>0)//temp>0代表搜索的路径是可以通过的 break; ans=(ans+1)%4; } return temp; } int dfs2(int x,int y,int step) { if(x==ex&&y==ey) return step+1; if(x<0||x>=m||y<0||y>=n||map[x][y]=='#') return 0; ans=(ans+3)%4; int temp=0; while(1) { temp=dfs2(x+r[ans][0],y+r[ans][1],step+1); if(temp>0) break; ans=(ans+1)%4; } return temp; } struct node { int x,y,step; }; int go(int x,int y) { if(x>=0&&x<m&&y>=0&&y<n&&!b[x][y]) return 1; return 0; } int bfs(int x,int y) { node st,ed; queue<node>q; st.x=x; st.y=y; st.step=1; b[st.x][st.y]=1; q.push(st); while(!q.empty()) { st=q.front(); q.pop(); if(map[st.x][st.y]=='E') return st.step; for(int k=0; k<4; k++) { ed.x=st.x+l[k][0]; ed.y=st.y+l[k][1]; if(go(ed.x,ed.y)&&map[ed.x][ed.y]!='#') { b[ed.x][ed.y]=1; ed.step=st.step+1; q.push(ed); } } } } int main() { scanf("%d",&t); while(t--) { int i,j; memset(b,0,sizeof(b)); scanf("%d%d",&n,&m); for(i=0; i<m; i++) scanf("%s",map[i]); for(i=0; i<m; i++) { for(j=0; j<n; j++) { if(map[i][j]=='S') { sx=i; sy=j; } if(map[i][j]=='E') { ex=i; ey=j; } } } ans=0; printf("%d ",dfs1(sx,sy,0)); ans=0; printf("%d ",dfs2(sx,sy,0)); printf("%d ",bfs(sx,sy)); } return 0; }