Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13225 | Accepted: 5724 |
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
一开始题意没读懂尴尬;
说的是万圣节走迷宫;
有两中走法:一种贴着左边的墙壁走,一种是贴着右边的墙壁走;
让你求贴左墙壁走的步数,右墙壁走的步数,和最短路数
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
char Map[50][50];
int vist[50][50],W,H,x,y,num1,num2,num3,fan;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
struct node
{
int row,col,step;
}now,next;
void DFS1(int X,int Y,int Fan)//贴左墙壁
{
num1++;
if(Map[X][Y] == 'E')
return;
switch(Fan)
{
case 0:
{
if(Map[X][Y-1] != '#')//左边可以走,往左走,当前方向改变
DFS1(X,Y-1,3);
else
{
if(Map[X-1][Y] != '#')//左边不可以走,当前方向可以走,往前走一步
DFS1(X-1,Y,0);
else
{
if(Map[X][Y+1] != '#')//左边。前面都不可以走,右边可以走,往右走,改变方向
DFS1(X,Y+1,1);
else//左边。前面,右边都不可以走,只能往后走,改变方向
DFS1(X+1,Y,2);
}
}
break;
}
case 1:
{
if(Map[X-1][Y] != '#')
DFS1(X-1,Y,0);
else
{
if(Map[X][Y+1] != '#')
DFS1(X,Y+1,1);
else
{
if(Map[X+1][Y] != '#')
DFS1(X+1,Y,2);
else
DFS1(X,Y-1,3);
}
}
break;
}
case 2:
{
if(Map[X][Y+1] != '#')
DFS1(X,Y+1,1);
else
{
if(Map[X+1][Y] != '#')
DFS1(X+1,Y,2);
else
{
if(Map[X][Y-1] != '#')
DFS1(X,Y-1,3);
else
DFS1(X-1,Y,0);
}
}
break;
}
case 3:
{
if(Map[X+1][Y] != '#')
DFS1(X+1,Y,2);
else
{
if(Map[X][Y-1] != '#')
DFS1(X,Y-1,3);
else
{
if(Map[X-1][Y] != '#')
DFS1(X-1,Y,0);
else
DFS1(X,Y+1,1);
}
}
break;
}
}
}
void DFS2(int X,int Y,int Fan)//贴右墙壁
{
num2++;
if(Map[X][Y] == 'E')
return;
switch(Fan)
{
case 0:
{
if(Map[X][Y+1] != '#')
DFS2(X,Y+1,1);
else
{
if(Map[X-1][Y] != '#')
DFS2(X-1,Y,0);
else
{
if(Map[X][Y-1] != '#')
DFS2(X,Y-1,3);
else
DFS2(X+1,Y,2);
}
}
break;
}
case 1:
{
if(Map[X+1][Y] != '#')
DFS2(X+1,Y,2);
else
{
if(Map[X][Y+1] != '#')
DFS2(X,Y+1,1);
else
{
if(Map[X-1][Y] != '#')
DFS2(X-1,Y,0);
else
DFS2(X,Y-1,3);
}
}
break;
}
case 2:
{
if(Map[X][Y-1] != '#')
DFS2(X,Y-1,3);
else
{
if(Map[X+1][Y] != '#')
DFS2(X+1,Y,2);
else
{
if(Map[X][Y+1] != '#')
DFS2(X,Y+1,1);
else
DFS2(X-1,Y,0);
}
}
break;
}
case 3:
{
if(Map[X-1][Y] != '#')
DFS2(X-1,Y,0);
else
{
if(Map[X][Y-1] != '#')
DFS2(X,Y-1,3);
else
{
if(Map[X+1][Y] != '#')
DFS2(X+1,Y,2);
else
DFS2(X,Y+1,1);
}
}
break;
}
}
}
void BFS(int X,int Y)//BFS求最短步数
{
memset(vist,0,sizeof(vist));
queue<node>Q;
now.row = X;
now.col = Y;
vist[now.row][now.col] = 1;
now.step = 1;
Q.push(now);
int flag = 0;
while(!Q.empty())
{
if(flag)
break;
now = Q.front();
Q.pop();
for(int i = 0; i < 4; i++)
{
next.row = now.row + dx[i];
next.col = now.col + dy[i];
if(next.row>=0 && next.row<H && next.col>=0 && next.col<W && !vist[next.row][next.col] && Map[next.row][next.col] != '#')
{
next.step = now.step + 1;
vist[next.row][next.col] = 1;
if(Map[next.row][next.col] == 'E')
{
flag = 1;
num3 = next.step;
break;
}
Q.push(next);
}
}
}
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&W,&H);
for(i = 0; i < H; i++)
{
scanf("%s",Map[i]);
for(j = 0; j < W; j++)
{
if(Map[i][j] == 'S')
{
x = i;
y = j;
if(x == H-1)
fan = 0;
else if(x == 0)
fan = 2;
else if(y == 0)
fan = 1;
else
fan = 3;
}
}
}
num1 = num2 = num3 = 0;
DFS1(x,y,fan);
DFS2(x,y,fan);
BFS(x,y);
printf("%d %d %d\n",num1,num2,num3);
}
return 0;
}