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
题目大意:说有一个迷宫,迷宫有一个入口S和出口E,现在要你求出三种路径各自的长度
1. 沿着最左边走。
2. 沿着最右边走。
3. 最短路径。
其实沿着最左,最右方向走的时候,特别需要考虑在顺时针和逆时针转的时候,当前方向,选择下一个位置,和下一个方向之间的关系。
起点方向由起点决定#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
char map[41][41];
int n,m;
bool visit[41][41];
int pos_x,pos_y;
int dir_left[4][2] = {{0,-1},{1,0},{0,1},{-1,0}} ; //向左优先 左 下 右 上 逆时针
int dir_right[4][2] = {{0,1},{1,0},{0,-1},{-1,0}} ; //向右优先 右 下 左 上 顺时针
struct node
{
int i;
int j;
int step;
};
bool judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&map[x][y]!='#'&&y<m&&!visit[x][y])
{
return true;
}
return false;
}
int bfs(int dir[][2])
{
int Min=1000000;
node h,t;
h.i=pos_x;
h.j=pos_y;
h.step=0;
queue<node>q;
q.push(h);
visit[h.i][h.j]=true;
while (!q.empty())
{
h=q.front();
q.pop();
for (int i=0; i<4; i++)
{
t=h;
t.i+=dir[i][0];
t.j+=dir[i][1];
if(judge(t.i, t.j))
{
t.step++;
if(map[t.i][t.j]=='E')
{
Min=min(t.step,Min);
}
else
{
q.push(t);
visit[t.i][t.j]=true;
}
}
}
}
return Min;
}
int cnt=0;
int dfs(int x,int y,int k,int dir[][2])
{
if(map[x][y]=='E')
{
return 1;
}
for (int i=0; i<4; i++)
{
int j=(k+i)%4;
int pos_x=x+dir[j][1];
int pos_y=y+dir[j][0];
if(judge(pos_x, pos_y))
{
cnt=dfs(pos_x,pos_y,(j+3)%4,dir)+1;
break;
}
}
return cnt;
}
int main()
{
int ans;
scanf("%d",&ans);
while (ans--)
{
scanf("%d%d",&m,&n);
cnt=0;
for (int i=0; i<n; i++)
{
scanf("%s",map[i]);
for (int j=0; j<m; j++)
{
if(map[i][j]=='S')
{
pos_x=i;
pos_y=j;
}
}
}
int d1,d2;
if(pos_x==0)
{
d1 = 1 ;
d2 = 1 ;
}
else if(pos_x== n-1)
{
d1 = 3 ;
d2 = 3 ;
}
else if(pos_y == m-1)
{
d1 = 0 ;
d2 = 2 ;
}
else
{
d1 = 2;
d2 = 0;
}
memset(visit, false, sizeof(visit));
cout<<dfs(pos_x, pos_y,d1,dir_left)<<" ";
cout<<dfs(pos_x, pos_y,d2,dir_right)<<" ";
cout<<bfs(dir_right)+1<<endl;
}
return 0;
}