Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9592 | Accepted: 4158 |
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
这题也是水题,只是比较麻烦,方向是动态变化的,而且要用到bfs和dfs。
题目要求输出向右,向左走的步数,及最短路。其中起点终点都在边界上,且不在对角线上。
则在四个不同的边界,面的朝向也随之改变。总的来说向左为顺时针,向右为逆时针,且每次方向的转变,必然伴随朝向的转变,就像人走路一样。
#include #include #include using namespace std; const int MAX=60; char map[MAX][MAX]; int vis[MAX][MAX];/*标记数组*/ int m,n,x1,y1,x2,y2; int shortest[MAX][MAX]; int stepl,stepr; int bx[]={0,0,-1,1}; int by[]={-1,1,0,0}; struct Node { int x,y; Node(){ } Node(int x,int y){ this->x=x; this->y=y; } }q[MAX*MAX]; /*向右*/ void dfsr(int sx,int sy,int r) { stepr++; if(sx == x2 && sy == y2) return; if(r==0){ if(vis[sx][sy+1]==0){ dfsr(sx,sy+1,3);/*原本面朝上,向右时,面朝右*/ } else if(vis[sx-1][sy]==0){ dfsr(sx-1,sy,0); } else if(vis[sx][sy-1]==0){ dfsr(sx,sy-1,1); } else if(vis[sx+1][sy]==0) dfsr(sx+1,sy,2); } else if(r==1){ if(vis[sx-1][sy]==0) dfsr(sx-1,sy,0); else if(vis[sx][sy-1]==0) dfsr(sx,sy-1,1); else if(vis[sx+1][sy]==0) dfsr(sx+1,sy,2); else if(vis[sx][sy+1]==0) dfsr(sx,sy+1,3); } else if(r==2){ if(vis[sx][sy-1]==0) dfsr(sx,sy-1,1); else if(vis[sx+1][sy]==0) dfsr(sx+1,sy,2); else if(vis[sx][sy+1]==0) dfsr(sx,sy+1,3); else if(vis[sx-1][sy]==0) dfsr(sx-1,sy,0); } else if(r==3){ if(vis[sx+1][sy]==0) dfsr(sx+1,sy,2); else if(vis[sx][sy+1]==0) dfsr(sx,sy+1,3); else if(vis[sx-1][sy]==0) dfsr(sx-1,sy,0); else if(vis[sx][sy-1]==0) dfsr(sx,sy-1,1); } } /*向左*/ void dfsl(int xx,int yy,int l) { stepl++; if(xx == x2 && yy == y2) return; if(l==0){ if(vis[xx][yy-1] == 0){ dfsl(xx,yy-1,1); } else if(vis[xx-1][yy] == 0){ dfsl(xx-1,yy,0); } else if(vis[xx][yy+1] == 0){ dfsl(xx,yy+1,3); } else if(vis[xx+1][yy] ==0){ dfsl(xx+1,yy,2); } } else if(l==1){ if(vis[xx+1][yy] ==0){ dfsl(xx+1,yy,2); } else if(vis[xx][yy-1] == 0){ dfsl(xx,yy-1,1); } else if(vis[xx-1][yy] == 0){ dfsl(xx-1,yy,0); } else if(vis[xx][yy+1] == 0){ dfsl(xx,yy+1,3); } } else if(l==2){ if(vis[xx][yy+1] == 0){ dfsl(xx,yy+1,3); } else if(vis[xx+1][yy] == 0){ dfsl(xx+1,yy,2); } else if(vis[xx][yy-1] == 0){ dfsl(xx,yy-1,1); } else if(vis[xx-1][yy] == 0){ dfsl(xx-1,yy,0); } } else if(l==3){ if(vis[xx-1][yy] == 0){ dfsl(xx-1,yy,0); } else if(vis[xx][yy+1] == 0){ dfsl(xx,yy+1,3); } else if(vis[xx+1][yy] == 0){ dfsl(xx+1,yy,2); } else if(vis[xx][yy-1] == 0){ dfsl(xx,yy-1,1); } } } void bfs(int sx,int sy) { int begin=0,end=0; q[end++]=Node(sx,sy); map[sx][sy]='#'; shortest[sx][sy] = 1; while(begin n||yy<=0||yy>m||map[xx][yy]=='#') continue; map[xx][yy] = '#'; q[end++]=Node(xx,yy); shortest[xx][yy]=shortest[now.x][now.y]+1; } } } int main() { int T,dir; cin>>T; while(T--) { stepr=stepl=0; memset(vis,-1,sizeof(vis)); cin>>m>>n; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ cin>>map[i][j]; if(map[i][j]=='S'){ x1=i; y1=j; vis[i][j] = 0; if(x1==n) dir=0; else if(y1 == m) dir=1; else if(x1 == 1) dir=2; else if(y1 == 1) dir=3; } if(map[i][j == 'E'){ x2=i; y2=j; vis[i][j] = 0; } if(map[i][j] == '.') vis[i][j] = 0; } dfsl(x1,y1,dir); dfsr(x1,y1,dir); bfs(x1,y1); cout< <<" "< <<" "< <