链接:http://poj.org/problem?id=3083
题目:
Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14487 | Accepted: 6253 |
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
题意:给一个n*m的迷宫,'#'是墙,'.'是空地,S是起点,E是终点。输出一行三个数字分别是:"优先左转"方式到达终点需要的步数、“优先右转”方式到达终点需要的步数、最短步数。
什么是“优先左转”?设一共有上下左右四个方向,假设从上一步到当前格子的方向是向右,那么下次就优先走当前方向的左方向(这里即向上)。如果向上不能走,那么就按照“上右下左”的优先顺序决定下一步的方向。同理,“优先右转”即按照“下右上左”的顺序决定方向。这里题目保证起点一定在迷宫的边界且不在四角上,所以可以根据起点坐标确定进入迷宫时的方向。
思路:显然,前面两个情况只需要执行dfs的时候设定好方向顺序就行了,最短步数做一次bfs即可。
代码中使用d[4][2]数组表示向四个方向的坐标改变,第一维下标0123分别对应上右下左四个方向。turns[2][4]数组分别代表两种方式下方向值的改变量。这里是精简代码的关键,不太好解释,但看代码应该能明白。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<list>
#include<queue>
#include<stack>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 100005;
char g[41][41];
int d[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};//上、右、下、左
int sx, sy, n, m;
int bfs(){
int step[41][41];
memset(step, 0, sizeof(step));
step[sx][sy] = 1;
queue<pair<int, int> > q;
q.push(make_pair(sx, sy));
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
if(g[x][y] == 'E'){
return step[x][y];
}
for(int i = 0; i < 4; ++i){
int dx = x + d[i][0], dy = y + d[i][1];
if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && step[dx][dy] == 0){
step[dx][dy] = step[x][y] + 1;
q.push(make_pair(dx, dy));
}
}
}
}
int turns[2][4] = {3, 0, 1, 2, 1, 0, 3, 2};
void dfs(int x, int y, int k, int dir, int step, int &ans){
//printf("x=%d y=%d k=%d dir=%d step=%d ans=%d\n", x, y, k, dir, step, ans);
if(g[x][y] == 'E'){
ans = step;
return ;
}
for(int i = 0; i < 4; ++i){
if(ans != 0){
return ;
}
int kk = (k + turns[dir][i]) % 4;
int dx = x + d[kk][0];
int dy = y + d[kk][1];
//printf("dx=%d dy=%d kk=%d\n", dx, dy, kk);
if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#'){
dfs(dx, dy, kk, dir, step + 1, ans);
}
}
}
int main(){
int t;
char ch;
scanf("%d", &t);
while(t--){
scanf("%d%d", &m, &n);
getchar();
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
scanf("%c", &g[i][j]);
if(g[i][j] == 'S'){
sx = i, sy = j;//起点坐标
}
}
getchar();
}
int k = sx == 0 ? 2 : sx == n - 1 ? 0 : sy == 0 ? 1 : 3;//初始方向
int lans = 0;
dfs(sx, sy, k, 0, 1, lans);
int rans = 0;
dfs(sx, sy, k, 1, 1, rans);
printf("%d %d %d\n", lans, rans, bfs());
}
return 0;
}