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.
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.
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’, 接下来一行每行输入三个数,第一个数表示方向一直向左走,走出迷宫所需要的步数,第二个表示,方向一字向右走,走出迷宫所需要的步数,第三个就是最小的步数; 以下图解:
思路:对于前两个来说,我们可以用两个 DFS 来求出,走出迷宫所需要的步数,第三种情况就需要用到,BFS 了,直接求出来最小的步数即可; 不过这里我看过很多都是列了两个dfs,不过看到了一个利用q(朝向)和rl(指定左优先还是右优先)之后合成里一个函数的博客。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int mov[4][2] = { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } };
char map[45][45];
int vis[45][45], d[45][45];
int n, m, res1, res2, res3;
struct Point {
int x, y;
}s, e;
int dfs(Point p, int q, int rl) //q为当前朝向,rl为左右优先
{
if (p.x == e.x&&p.y == e.y)
return 1;
int res = 0;
for (int i = q + rl; i < q + rl + 4 * (4 - rl); i += 4 - rl)
{
int tmpx, tmpy;
tmpx = p.x + mov[i % 4][0], tmpy = p.y + mov[i % 4][1];
if (map[tmpx][tmpy] == '.')
{
Point next = { tmpx,tmpy };
res = dfs(next, i % 4, rl) + 1;
break; //注意一定要break因为,只要能找到就不再搜下一个方向
}
}
return res;
}
int bfs() //bfs求最短路,dfs会超时
{
d[s.x][s.y] = 0;
queue<Point> q;
q.push(s);
vis[s.x][s.y] = 1;
while (!q.empty())
{
Point v = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int tmpx = v.x + mov[i][0], tmpy = v.y + mov[i][1];
if (map[tmpx][tmpy] == '.'&&vis[tmpx][tmpy] == 0)
{
Point next = { tmpx,tmpy };
q.push(next);
d[tmpx][tmpy] = d[v.x][v.y] + 1;
if (tmpx == e.x&&tmpy == e.y)
return d[tmpx][tmpy];
vis[tmpx][tmpy] = 1;
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(d, 0x3f3f3f3f, sizeof(vis));
memset(vis, 0, sizeof(vis));
scanf("%d%d", &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')
{
s.x = i, s.y = j;
}
else if (map[i][j] == 'E')
{
e.x = i, e.y = j;
map[i][j] = '.'; //这一步别漏了
}
}
}
int q;
if (s.x == 1) q = 3; //确认处于S处的初始面向
else if (s.x == n) q = 1;
else if (s.y == 1) q = 2;
else q = 0;
res1 = dfs(s, q, 3);
res2 = dfs(s, q, 1);
res3 = bfs() + 1;
printf("%d %d %d\n", res1, res2, res3);
}
return 0;
}