1401. Children of the Candy Corn
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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
题目分析
假设单入口单出口的迷宫,且保证顺着左手和右手方向都一定能走出去
假设初始面向北方
注意移动到下一个格子时的朝向,以此获得此时的遍历顺序
#include <iostream>
#include <queue>
char palace[41][41];
int sr, sc, er, ec;
int row, col;
int left, right, shortest;
struct Pos {
int r, c;
Pos(int rr, int cc) {
r = rr;
c = cc;
}
};
void goleft() {
left = 1;
int dir[4][2] = {0,-1, -1,0, 0,1, 1,0};
int face = 0;
int nowr = sr;
int nowc = sc;
int index;
while (!(nowr==er && nowc==ec)) {
//std::cout << nowr << " " << nowc << std::endl;
for (int i = 0; i < 4; ++i) {
index = (face+i)%4;
if (nowr+dir[index][0] < 0 || nowr+dir[index][0] >=row)
continue;
if (nowc+dir[index][1] < 0 || nowc+dir[index][1] >=col)
continue;
if (palace[ nowr+dir[index][0] ][ nowc+dir[index][1] ] == '#')
continue;
nowr+=dir[index][0];
nowc+=dir[index][1];
++left;
face = index==0?3:index-1;
break;
}
}
}
void goright() {
right = 1;
int dir[4][2] = {0,1, -1,0, 0,-1, 1,0};
int face = 0;
int nowr = sr;
int nowc = sc;
int index;
while (!(nowr==er && nowc==ec)) {
//std::cout << nowr << " " << nowc << std::endl;
for (int i = 0; i < 4; ++i) {
index = (face+i)%4;
if (nowr+dir[index][0] < 0 || nowr+dir[index][0] >=row)
continue;
if (nowc+dir[index][1] < 0 || nowc+dir[index][1] >=col)
continue;
if (palace[ nowr+dir[index][0] ][ nowc+dir[index][1] ] == '#')
continue;
nowr+=dir[index][0];
nowc+=dir[index][1];
++right;
face = index==0?3:index-1;
break;
}
}
}
void bfs() {
int dir[4][2] = {-1,0, 0,-1, 0,1, 1,0};
std::queue<Pos> q;
q.push(Pos(sr,sc));
palace[sr][sc] = '#';
std::queue<int> steps;
steps.push(1);
while (!q.empty()) {
Pos now = q.front();
//std::cout << now.r << " " << now.c << std::endl;
q.pop();
int nowstep = steps.front();
steps.pop();
int tempr, tempc;
for (int i = 0; i < 4; ++i) {
tempr = now.r+dir[i][0];
tempc = now.c+dir[i][1];
if (tempr < 0 || tempr >= row || tempc < 0 || tempc >= col)
continue;
if (tempr == er && tempc == ec) {
shortest = nowstep+1;
return;
}
if (palace[tempr][tempc] == '#')
continue;
q.push(Pos(tempr, tempc));
steps.push(nowstep+1);
palace[tempr][tempc] = '#';
}
}
}
int main()
{
int num;
std::cin >> num;
while (num--) {
std::cin >> col >> row;
std::string str;
for (int i = 0; i < row; ++i) {
std::cin >> str;
for (int j = 0; j < col; ++j) {
palace[i][j] = str[j];
if (palace[i][j] == 'S') {
palace[i][j] = '.';
sr = i; sc = j;
} else if (palace[i][j] == 'E') {
palace[i][j] = '.';
er = i; ec = j;
}
}
}
goleft();
goright();
bfs();
std::cout << left << " " << right << " " << shortest << std::endl;
}
}