Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8986 | Accepted: 3919 |
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
/*
主要是题目问了三个问题
新意在总是沿着墙朝左或右走,所以需要一个face记录当前面朝哪面
我的代码也很长,感觉虽然写法麻烦(dfsl和dfsr里面相似的地方有很多,可以直接复制粘贴然后再改一改),但思路清晰就行了
方向: 3
2 + 4 每个点的坐标在平面直角坐标系第一象限,最左下角的点设为(0,0)
1
*/
#include<iostream>
#include<cstring>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int map[50][50];
bool v[50][50];
int sx,sy,ex,ey;
int flag;
int ans,ansl,ansr;
struct node
{
int x,y,step;
}m[2000];
queue<node>q;
void dfsl(int x,int y,int step,int face)
{
if(flag==1) return;
if(x==ex&&y==ey)
{
flag=1;
ansl=step;
return;
}
if(face==1)
{
if(map[x+1][y]) dfsl(x+1,y,step+1,4);
else if(map[x][y-1]) dfsl(x,y-1,step+1,1);
else if(map[x-1][y]) dfsl(x-1,y,step+1,2);
else if(map[x][y+1]) dfsl(x,y+1,step+1,3);
}
if(face==2)
{
if(map[x][y-1]) dfsl(x,y-1,step+1,1);
else if(map[x-1][y]) dfsl(x-1,y,step+1,2);
else if(map[x][y+1]) dfsl(x,y+1,step+1,3);
else if(map[x+1][y]) dfsl(x+1,y,step+1,4);
}
if(face==3)
{
if(map[x-1][y]) dfsl(x-1,y,step+1,2);
else if(map[x][y+1]) dfsl(x,y+1,step+1,3);
else if(map[x+1][y]) dfsl(x+1,y,step+1,4);
else if(map[x][y-1]) dfsl(x,y-1,step+1,1);
}
if(face==4)
{
if(map[x][y+1]) dfsl(x,y+1,step+1,3);
else if(map[x+1][y]) dfsl(x+1,y,step+1,4);
else if(map[x][y-1]) dfsl(x,y-1,step+1,1);
else if(map[x-1][y]) dfsl(x-1,y,step+1,2);
}
}
void dfsr(int x,int y,int step,int face)
{
if(flag==1) return;
if(x==ex&&y==ey)
{
flag=1;
ansr=step;
return;
}
if(face==1)
{
if(map[x-1][y]) dfsr(x-1,y,step+1,2);
else if(map[x][y-1]) dfsr(x,y-1,step+1,1);
else if(map[x+1][y]) dfsr(x+1,y,step+1,4);
else if(map[x][y+1]) dfsr(x,y+1,step+1,3);
}
if(face==2)
{
if(map[x][y+1]) dfsr(x,y+1,step+1,3);
else if(map[x-1][y]) dfsr(x-1,y,step+1,2);
else if(map[x][y-1]) dfsr(x,y-1,step+1,1);
else if(map[x+1][y]) dfsr(x+1,y,step+1,4);
}
if(face==3)
{
if(map[x+1][y]) dfsr(x+1,y,step+1,4);
else if(map[x][y+1]) dfsr(x,y+1,step+1,3);
else if(map[x-1][y]) dfsr(x-1,y,step+1,2);
else if(map[x][y-1]) dfsr(x,y-1,step+1,1);
}
if(face==4)
{
if(map[x][y-1]) dfsr(x,y-1,step+1,1);
else if(map[x+1][y]) dfsr(x+1,y,step+1,4);
else if(map[x][y+1]) dfsr(x,y+1,step+1,3);
else if(map[x-1][y]) dfsr(x-1,y,step+1,2);
}
}
void bfs()//求最短路径
{
int x,y,step;
int count=1;
memset(v,0,sizeof(v));
v[sx][sy]=1;
m[0].x=sx;
m[0].y=sy;
m[0].step=1;
q.push(m[0]);
while(q.size())
{
x=q.front().x;
y=q.front().y;
step=q.front().step;
q.pop();
if(x==ex&&y==ey)
{
ans=step;
return;
}
if(!v[x][y-1]&&map[x][y-1])
{
v[x][y-1]=1;
m[count].x=x;
m[count].y=y-1;
m[count].step=step+1;
q.push(m[count]);
count++;
}
if(map[x-1][y]&&!v[x-1][y])
{
v[x-1][y]=1;
m[count].x=x-1;
m[count].y=y;
m[count].step=step+1;
q.push(m[count]);
count++;
}
if(map[x][y+1]&&!v[x][y+1])
{
v[x][y+1]=1;
m[count].x=x;
m[count].y=y+1;
m[count].step=step+1;
q.push(m[count]);
count++;
}
if(map[x+1][y]&&!v[x+1][y])
{
v[x+1][y]=1;
m[count].x=x+1;
m[count].y=y;
m[count].step=step+1;
q.push(m[count]);
count++;
}
}
}
int main()
{
int t,w,h,i,j;
char str[50];
cin>>t;
while(t--)
{
while(q.size()) q.pop();//使用队列前应该清空队列!
memset(map,0,sizeof(map));
cin>>w>>h;
getchar();//接收回车,因为回车也是一个字符
for(i=1;i<=h;i++)
{
gets(str);
for(j=0;j<w;j++)
{
if(str[j]=='.') map[i][j+1]=1;
if(str[j]=='S')
{
map[i][j+1]=1;
sx=i;
sy=j+1;
}
if(str[j]=='E')
{
map[i][j+1]=1;
ex=i;
ey=j+1;
}
}
}
flag=0;
dfsl(sx,sy,1,1);
flag=0;
dfsr(sx,sy,1,1);
bfs();
cout<<ansl<<' '<<ansr<<' '<<ans<<endl;
}
return 0;
}