Children of the Candy Corn
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
Source
South Central USA 2006
/*
题意叙述还算清楚,给出一个N(长)*M(宽)的地图,S和E的起点保证都在边框上,然后问从S开始以左为优先方向需要多少步才能到达E,S开始以右为优先方向需要多少步才能到达E,S到E的最近步数有多少步。
解题思路,可以对于前两个有左右方向的搜寻,可以按照DFS算法进行,而最近的步数则用BFS算法进行。
*/
/*
题意叙述还算清楚,给出一个N(长)*M(宽)的地图,S和E的起点保证都在边框上,然后问从S开始以左为优先方向需要多少步才能到达E,S开始以右为优先方向需要多少步才能到达E,S到E的最近步数有多少步。
解题思路,可以对于前两个有左右方向的搜寻,可以按照DFS算法进行,而最近的步数则用BFS算法进行。
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
char Map[110][110];
int n,m;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
void L_dfs(int fx,int st,int x,int y)
{
if(Map[x][y] == 'E')
{
cout<<st<<" ";
return;
}
fx--;
if(fx < 0)
fx = 3;
int dl,lx,ly;
for(int i=0;i<4;i++)
{
dl = fx + i;
if(dl >= 4)
dl-=4;
lx = x + dx[dl];
ly = y + dy[dl];
if(lx >= 0 && lx < m && ly >=0 && ly < n && Map[lx][ly]!='#')
{
L_dfs(dl,st+1,lx,ly);
break;
}
}
}
void R_dfs(int fx,int st,int x,int y)
{
if(Map[x][y] == 'E')
{
cout<<st<<" ";
return;
}
fx++;
fx%=4;
int dl,lx,ly;
for(int i=0;i<4;i++)
{
dl = fx - i;
if(dl < 0)
dl += 4;
lx = x + dx[dl];
ly = y + dy[dl];
if(lx >= 0 && lx < m && ly >=0 && ly < n && Map[lx][ly]!='#')
{
R_dfs(dl,st+1,lx,ly);
break;
}
}
}
struct node
{
int x,y;
};
int vis[110][110];
void bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
queue<node>s;
s.push({x,y});
vis[x][y] = 1;
while(!s.empty())
{
node ans = s.front();
s.pop();
if(Map[ans.x][ans.y] == 'E')
{
cout<<vis[ans.x][ans.y]<<endl;
return;
}
for(int i=0;i<4;i++)
{
int lx = ans.x + dx[i];
int ly = ans.y + dy[i];
if(lx >= 0 && lx < m && ly >= 0 && ly < n && !vis[lx][ly] && Map[lx][ly]!='#')
{
vis[lx][ly] = vis[ans.x][ans.y] + 1;
s.push({lx,ly});
}
}
}
}
int main()
{
int t,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%s",Map[i]);
for(int j=0;j<n;j++)
{
if(Map[i][j] == 'S')
{
x = i;
y = j;
}
}
}
L_dfs(0,1,x,y);//0为方向,1为起点步数,x和y代表起点位置
R_dfs(0,1,x,y);
bfs(x,y);
}
return 0;
}