Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12804 | Accepted: 5506 |
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
题意:一个迷宫,S是起点,E是终点,让你求左优先的,右优先和最少到达终点的步数
左优先:先向左转,然后向前,再向右,最后向后
左优先:先向右转,然后向前,再向左,最后向后
这两步用dfs,方向是面朝的方向
2
1 × 3
4
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;
int fx[]={0,-1,0,1};
int fy[]={1,0,-1,0};
char mp[50][50];
int v[50][50];
int x1,y1;
int n,m;
int flag;
struct node
{
int x,y,step;
};
int f(int x,int y)
{
if(mp[x][y]!='#' && x>=0 && x<n && y>=0 && y<m)
{
return 1;
}
else
return 0;
}
void bfs()
{
memset(v,0,sizeof(v));
struct node l,k;
l.x=x1;
l.y=y1;
l.step=1;
queue<node>q;
while(!q.empty())q.pop();
q.push(l);
v[x1][y1]=1;
while(!q.empty())
{
k=q.front();
q.pop();
if(mp[k.x][k.y]=='E')
{
printf("%d\n",k.step);
return ;
}
for(int i=0;i<4;i++)
{
int xx=k.x+fx[i];
int yy=k.y+fy[i];
if((mp[xx][yy]=='.' || mp[xx][yy]=='E') && !v[xx][yy])
{
struct node tep;
tep.x=xx;
tep.y=yy;
tep.step=k.step+1;
v[xx][yy]=1;
q.push(tep);
}
}
}
}
void zuo(int x,int y,int step,int k)
{
if(flag)return ;
if(mp[x][y]=='E')
{
flag=1;
printf("%d ",step);
return ;
}
if(k==1)
{
if(f(x+1,y)){zuo(x+1,y,step+1,4);}
else if(f(x,y-1)){zuo(x,y-1,step+1,1);}
else if(f(x-1,y)){zuo(x-1,y,step+1,2);}
else if(f(x,y+1)){zuo(x,y+1,step+1,3);}
}
if(k==2)
{
if(f(x,y-1)){zuo(x,y-1,step+1,1);}
else if(f(x-1,y)){zuo(x-1,y,step+1,2);}
else if(f(x,y+1)){zuo(x,y+1,step+1,3);}
else if(f(x+1,y)){zuo(x+1,y,step+1,4);}
}
if(k==3)
{
if(f(x-1,y)){zuo(x-1,y,step+1,2);}
else if(f(x,y+1)){zuo(x,y+1,step+1,3);}
else if(f(x+1,y)){zuo(x+1,y,step+1,4);}
else if(f(x,y-1)){zuo(x,y-1,step+1,1);}
}
if(k==4)
{
if(f(x,y+1)){zuo(x,y+1,step+1,3);}
else if(f(x+1,y)){zuo(x+1,y,step+1,4);}
else if(f(x,y-1)){zuo(x,y-1,step+1,1);}
else if(f(x-1,y)){zuo(x-1,y,step+1,2);}
}
}
void you(int x,int y,int step,int k)
{
if(flag)return ;
if(mp[x][y]=='E')
{
flag=1;
printf("%d ",step);
return ;
}
if(k==1)
{
if(f(x-1,y)){you(x-1,y,step+1,2);}
else if(f(x,y-1)){you(x,y-1,step+1,1);}
else if(f(x+1,y)){you(x+1,y,step+1,4);}
else if(f(x,y+1)){you(x,y+1,step+1,3);}
}
if(k==2)
{
if(f(x,y+1)){you(x,y+1,step+1,3);}
else if(f(x-1,y)){you(x-1,y,step+1,2);}
else if(f(x,y-1)){you(x,y-1,step+1,1);}
else if(f(x+1,y)){you(x+1,y,step+1,4);}
}
if(k==3)
{
if(f(x+1,y)){you(x+1,y,step+1,4);}
else if(f(x,y+1)){you(x,y+1,step+1,3);}
else if(f(x-1,y)){you(x-1,y,step+1,2);}
else if(f(x,y-1)){you(x,y-1,step+1,1);}
}
if(k==4)
{
if(f(x,y-1)){you(x,y-1,step+1,1);}
else if(f(x+1,y)){you(x+1,y,step+1,4);}
else if(f(x,y+1)){you(x,y+1,step+1,3);}
else if(f(x-1,y)){you(x-1,y,step+1,2);}
}
}
int main()
{
int i,j;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=0;i<n;i++)
{
scanf("%s",mp[i]);
for(j=0;j<m;j++)
{
if(mp[i][j]=='S')
{
x1=i;
y1=j;
}
}
}
flag=0;
zuo(x1,y1,1,1);
flag=0;
you(x1,y1,1,1);
bfs();
}
return 0;
}