文章转自:http://blog.csdn.net/u010660276/article/details/9567783
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. 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 Source |
题意:给你一个h * w的迷宫,其中有一句话需要注意一下“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.”即始点‘S’和终点‘E’一定都与迷宫的边界相邻,这对做题很关键。然后问你沿着迷宫墙壁的左边走和沿着迷宫墙壁的右边走,各需多少步,然后最少需要多少步。
思路:求最少步数没有什么好说当然是用bfs,重要的是求沿着迷宫墙壁走的两种状态。首先考虑,迷宫的起点是与墙壁挨着的,所以当S在迷宫最左边(sy == 0)如果沿着左边走,应该先考虑向上走的情况,如果向上走不通,在考虑像右走的情况,最后考虑向下走的情况;如果沿着右边走,应先考虑向下走的情况,再考虑向右走的情况,再考虑向上走的情况;其他的情况(sx == 0 || sy == w-1||sx == h - 1)时思路是一样的,自己推导吧~于是只要dfs枚举在每一个方向上可能有的各种情况就好了。又写了一遍这道题的代码 着道题我觉得重要的是方向的确立 与方向坐标的顺序 同时注意方向坐标 x,y分别表示的意思。 方向的确立的一种方法就是花一个正方形,若是右优先就是逆时针 ,把正方向的位子设为0 这个位置同时也是方向坐标(0,1)里面的位置,然后他的右边是1 既是上(1,0) 这样以此类推这种方向坐标位置的确立加方法无疑是这道题最精髓的东西。其实规定向左为顺时针或者逆时针是无所谓的,只要下次操作时与其一致就可以了,没必要那么纠结这个事情。 还要注意的事情就是当到底一个新的坐标的时候的时候,在这个新的坐标上也要考虑坐标移动的先后方向,按照以前设置好的方向进行考虑。
具体代码如下:
具体代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
int x, y;
int step;
};
char mp[50][50];
int flag[50][50];
int d1, d2;
int w, h;
int start[2], end[2];
int dirL[5][2] = {{0,-1},{1,0},{0,1},{-1,0}};
int dirR[5][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int dfs( int x, int y, int d, int dir[][2] )
{
int temp, tx, ty, step;
if( x==end[0]&&y==end[1] )
return 1;
for( int i = 0;i < 4;i++ )
{
temp = (d+i)%4;
tx = x + dir[temp][1];
ty = y + dir[temp][0];
if( tx>=0&&tx<h&&ty>=0&&ty<w&&!flag[tx][ty] )
break;
}
step = dfs(tx, ty, (temp+3)%4, dir) + 1;
return step;
}
int bfs()
{
memset(flag, 0, sizeof(flag));
queue<struct node> q;
while(!q.empty())
q.pop();
struct node p;
p.x = start[0];
p.y = start[1];
p.step = 1;
flag[p.x][p.y] = 1;
q.push(p);
while(!q.empty())
{
p = q.front();
q.pop();
if( p.x==end[0]&&p.y==end[1] )
return p.step;
for( int i = 0;i < 4;i++ )
{
struct node temp;
temp.x = p.x + dirL[i][1];
temp.y = p.y + dirL[i][0];
if( temp.x>=0&&temp.x<h&&temp.y>=0&&temp.y<w&&mp[temp.x][temp.y]!='#'&&!flag[temp.x][temp.y] )
{
flag[temp.x][temp.y] = 1;
temp.step = p.step + 1;
q.push(temp);
}
}
}
return 0;
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(flag, 0,sizeof(flag));
cin>>w>>h;
for( int i = 0;i < h;i++ )
{
cin>>mp[i];
for( int j = 0;j < w;j++ )
{
if( mp[i][j]=='#' )
flag[i][j] = 1;
else if( mp[i][j]=='S' )
{
start[0] = i;
start[1] = j;
}
else if( mp[i][j]=='E' )
{
end[0] = i;
end[1] = j;
}
}
}
if( start[0]==0 )
{
d1 = 1;
d2 = 1;
}
else if( start[0]==h-1 )
{
d1 = 3;
d2 = 3;
}
else if( start[1]==w-1 )
{
d1 = 2;
d2 = 0;
}
else
{
d1 = 0;
d2 = 2;
}
cout<<dfs(start[0], start[1], d1, dirL)<<" ";
cout<<dfs(start[0], start[1], d2, dirR)<<" ";
cout<<bfs()<<endl;
}
return 0;
}