POJ-3083

描述:

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 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.

输出:

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.

输入示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

输出示例:

1
2
37 5 5
17 17 9

题目大意:

有一张地图,# 是墙壁,. 是路,S是点,E是终点。(S和E在地图边缘且不在四个对角)

问:优先选择左边到达终点的路径长度(目前朝向的左边为优先选边,顺时针),优先选择右边到达终点的路径长度(目前朝向的右边为优先选边,逆时针),和最短的路径长度。

思路:

该题使用dfs(前两问)加bfs(后一问)来解答,由于有一个优先选择当前方向的左边或者右边位置,所以需要处理一个当前的朝向问题。比普通的dfs要复杂一点。但是根据定义的方向数组,只要知道了向左转就是当前的朝向减一个单位,右转为当前的朝向加一个单位,以此为起始探测方向,并且处理好顺逆时针关系就可以化解该题。

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int MAXN = 50;
char m[MAXN][MAXN];

int N,M;
int sx,sy,ex,ey;
int ans;
int flag;
struct node{
int x,y,d;
node(){}
node(int _x,int _y, int _d):x(_x),y(_y),d(_d){}
};

//从左顺时针
int dx[] = {0,-1,0,1};
int dy[] = {-1,0,1,0};


void dfs(int x, int y, int pos,int t,int s){
// printf("%d %d %d\\n",x,y,d);
if(flag) return;
if(x == ex && y == ey){
flag = 1;
ans = max(ans,s);
return;
}
//t = -1 表示顺时针,t = 1 表示逆时针
//pp : 当前的方向
//如果是左优先,就将当前方向-1开始顺时针,
//如果是右优先,就将当前方向+1开始逆时针。
for(int i = 1,pp = (pos+t+4)%4; i <= 4; i++,pp = (pp-t+4)%4){
int nx = dx[pp] + x;
int ny = dy[pp] + y;
if(nx >= 0 && nx < N && ny >= 0 && ny < M && m[nx][ny] == '.'){
dfs(nx,ny,pp,t,s+1);
if(flag) return;
}
}

}


void bfs(int x,int y){
queue<node> q;
q.push(node(x,y,1));
m[x][y] = '#';
node temp;
while(q.size()){
temp = q.front();q.pop();
if(temp.x == ex && temp.y == ey){
ans = temp.d;
return;
}
for(int i = 0; i < 4; i++){
int nx = dx[i] + temp.x;
int ny = dy[i] + temp.y;
if(nx >= 0 && nx < N && ny >= 0 && ny < M && m[nx][ny] == '.'){
m[nx][ny] = '#';
q.push(node(nx,ny,temp.d+1));
}
}
}
}

int main(){
int T;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--){
int pos = 0;
scanf("%d%d",&M,&N);
// memset(m,0,sizeof(m));
for(int i = 0; i < N; i++){
scanf("%s",m[i]);
for(int j = 0; j < M; j++){
if(m[i][j] == 'S'){
sx = i,sy = j; m[i][j] = '.';
}else if(m[i][j] == 'E'){
ex = i,ey = j; m[i][j] = '.';
}
}
}
// for(int i = 0; i < N; i++){
// for(int j = 0; j < M; j++){
// printf("%c",m[i][j]);
// }
// printf("\\n");
// }
/*
pos = 0 表示向上为初始方向
pos = 1 表示向右为初始方向
pos = 2 表示向下为初始方向
pos = 3 表示向左为初始方向
*/
if(sx == 0) pos = 2;
if(sx == N-1) pos = 0;
if(sy == 0) pos = 1;
if(sy == M-1) pos = 3;
ans = 0;
flag = 0;
dfs(sx,sy,pos,-1,1);
printf("%d",ans);
ans = 0;
flag = 0;
dfs(sx,sy,pos,1,1);
printf(" %d",ans);
ans = 0;
bfs(sx,sy);
printf(" %d\\n",ans);
}
return 0;
}