PO3083 DFS+BFS

来源:互联网 发布:淘宝详情页视频时间 编辑:程序博客网 时间:2024/05/17 04:08
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12992 Accepted: 5612

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

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########


题意:给定一个迷宫,要求从S走到E,分别输出“只能向前进方向左拐”“只能向前进方向右拐”“最短路径”三种路径长度。

思路:前两种是DFS的加强改版,后一种是裸BFS。

AC代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;struct ss{    int x,y,sn;};bool vis[50][50];char a[50][50];int n,m,xs,xe,ys,ye;int bfsp[4][2]={{-1,0},{1,0},{0,1},{0,-1}};int dfspl[4][2]={{0,-1},{-1,0},{0,1},{1,0}};int dfspr[4][2]={{0,1},{-1,0},{0,-1},{1,0}};int bfs(){    bool vis[50][50];    memset(vis,false,sizeof(vis));    queue<ss>q;    ss t0;    t0.x=xs;    t0.y=ys;    t0.sn=1;    q.push(t0);    vis[xs][ys]=true;    while(!q.empty())    {        ss t=q.front();        q.pop();        if(t.x==xe&&t.y==ye)            return t.sn;        for(int i=0;i<4;i++)        {            ss tt;            tt.x=t.x+bfsp[i][0];            tt.y=t.y+bfsp[i][1];            tt.sn=t.sn+1;            if(tt.x<=n&&tt.x>=1&&tt.y<=m&&tt.y>=1&&!vis[tt.x][tt.y]&&a[tt.x][tt.y]!='#')            {                vis[tt.x][tt.y]=true;                q.push(tt);            }        }    }    return 0;}int dfs(int x,int y,int d,int step,int path[][2]){    for(int i=0;i<4;i++)    {        int j=((d-1+4)%4+i)%4;        int xx=x+path[j][0];        int yy=y+path[j][1];        if(xx==xe&&yy==ye)            return step+1;        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]!='#')            return dfs(xx,yy,j,step+1,path);    }}int main(){    //freopen("D://in.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d %d",&m,&n);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                cin>>a[i][j];                if(a[i][j]=='S')                {                    xs=i;                    ys=j;                }                if(a[i][j]=='E')                {                    xe=i;                    ye=j;                }            }        }        int d1,d2;        if(xs==0)            d1=3,d2=3;        else if(xs==n-1)            d1=1,d2=1;        else if(ys==0)            d1=2,d2=0;        else if(ys==m-1)            d1=0,d2=2;        printf("%d %d %d\n",dfs(xs,ys,d1,1,dfspl),dfs(xs,ys,d2,1,dfspr),bfs());    }    return 0;}


0 0
原创粉丝点击