POJ3083——Children of the Candy Corn(DFS+BFS)

时间:2021-08-28 16:22:16

Children of the Candy Corn

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

题目大意:

    一个迷宫,'.','S','E'为可以到达的点,'#'为墙。 (S点保证在迷宫边缘,且只有一个方向可以走)

    规定了三种行动方式(计算从S->N的路程)

    1)优先走当前方向的左方,前方,右方,后方。

    2)优先走当前方向的右方,前方,左方,后方。

    3)S->E的最短路径。

解题思路:

    用DFS求前两个行动方式的解。

    使用0123来表示当前所在位置的方向。 0前 1右 2后 3左

    再通过当前的方向来确定递归的优先级。

    BFS求最短路径。

Code:

 #include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
#include<memory.h>
#include<algorithm>
#define MAXN 41
using namespace std;
struct qu
{
int x,y;
}q[];
int N,M,end_i,end_j;
bool vis[MAXN+][MAXN+],flag[MAXN+][MAXN+];
int dis[],Lstep,Rstep;
void dfs_left(int x1,int y1,int d)
{
Lstep++;
if (x1==end_i&&y1==end_j) return ;
if (d==)
{
if (flag[x1][y1-]) dfs_left(x1,y1-,);
else if (flag[x1-][y1]) dfs_left(x1-,y1,);
else if (flag[x1][y1+]) dfs_left(x1,y1+,);
else if (flag[x1+][y1]) dfs_left(x1+,y1,);
}
if (d==)
{
if (flag[x1-][y1]) dfs_left(x1-,y1,);
else if (flag[x1][y1+]) dfs_left(x1,y1+,);
else if (flag[x1+][y1]) dfs_left(x1+,y1,);
else if (flag[x1][y1-]) dfs_left(x1,y1-,);
}
if (d==)
{
if (flag[x1][y1+]) dfs_left(x1,y1+,);
else if (flag[x1+][y1]) dfs_left(x1+,y1,);
else if (flag[x1][y1-]) dfs_left(x1,y1-,);
else if (flag[x1-][y1]) dfs_left(x1-,y1,);
}
if (d==)
{
if (flag[x1+][y1]) dfs_left(x1+,y1,);
else if (flag[x1][y1-]) dfs_left(x1,y1-,);
else if (flag[x1-][y1]) dfs_left(x1-,y1,);
else if (flag[x1][y1+]) dfs_left(x1,y1+,);
}
}
void dfs_right(int x1,int y1,int d)
{
Rstep++;
if (x1==end_i&&y1==end_j) return ;
if (d==)
{
if (flag[x1][y1+]) dfs_right(x1,y1+,);
else if (flag[x1-][y1]) dfs_right(x1-,y1,);
else if (flag[x1][y1-]) dfs_right(x1,y1-,);
else if (flag[x1+][y1]) dfs_right(x1+,y1,);
}
if (d==)
{
if (flag[x1+][y1]) dfs_right(x1+,y1,);
else if (flag[x1][y1+]) dfs_right(x1,y1+,);
else if (flag[x1-][y1]) dfs_right(x1-,y1,);
else if (flag[x1][y1-]) dfs_right(x1,y1-,);
}
if (d==)
{
if (flag[x1][y1-]) dfs_right(x1,y1-,);
else if (flag[x1+][y1]) dfs_right(x1+,y1,);
else if (flag[x1][y1+]) dfs_right(x1,y1+,);
else if (flag[x1-][y1]) dfs_right(x1-,y1,);
}
if (d==)
{
if (flag[x1-][y1]) dfs_right(x1-,y1,);
else if (flag[x1][y1-]) dfs_right(x1,y1-,);
else if (flag[x1+][y1]) dfs_right(x1+,y1,);
else if (flag[x1][y1+]) dfs_right(x1,y1+,);
}
}
int bfs(int x1,int y1)
{
int front=,rear=;
dis[front]=;
q[front].x=x1,q[front].y=y1;
while (front<rear)
{
int x=q[front].x,y=q[front].y;
if (x==end_i&&y==end_j) break;
int a[]={x+,x-,x,x},b[]={y,y,y+,y-};
for (int i=;i<=;i++)
{
if (!(a[i]<=||a[i]>=M+||b[i]<=||b[i]>=N+)&&vis[a[i]][b[i]]==){
q[rear].x=a[i],q[rear].y=b[i];
dis[rear]=dis[front]+;
vis[a[i]][b[i]]=;
rear++;
}
}
front++;
}
return dis[front]; }
int main()
{
int T,start_i,start_j;
cin>>T;
while (T--)
{
Lstep=Rstep=;
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
memset(q,,sizeof(q));
int d;
cin>>N>>M;
getchar();
for (int i=; i<=M; i++)
{
for (int j=; j<=N; j++)
{
char tmp;
scanf("%c",&tmp);
flag[i][j]=;
if (tmp=='#') vis[i][j]=,flag[i][j]=;
if (tmp=='E') end_i=i,end_j=j;
if (tmp=='S') start_i=i,start_j=j;
}
getchar();
}
if (start_i==) d=;
else if (start_i==M) d=;
else if (start_j==) d=;
else if (start_j==N) d=;
dfs_left(start_i,start_j,d);
dfs_right(start_i,start_j,d);
int step1=bfs(start_i,start_j);
printf("%d %d %d\n",Lstep,Rstep,step1);
}
return ;
}