POJ 3083

来源:互联网 发布:e卡销外包源码 编辑:程序博客网 时间:2024/06/06 04:42
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9140 Accepted: 3965

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#.#.#.#.##########

Sample Output

37 5 517 17 9打死我都不会相信我会去写搜索,结果没打死,所以去写了,BFS和DFS的综合应用。BFS裸的求最短距离,DFS分左优先和右优先进行搜索,搜到最后的值,所以在DFS中开一个DIR用来记录当前方向,BFS的时候注意将VIS数组初始化和标记。。 在这里目测跪了好几次。。剩下的就是合法性的判断写好就好了。。。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<algorithm>using namespace std;char map[45][45];int m,n,sx,sy,ex,ey;int dir;int lx[4]={1,0,-1,0};int ly[4]={0,-1,0,1};int rx[4]={-1,0,1,0};int ry[4]={0,-1,0,1};int ans;struct node{int x;int y;int step;}q[100000];bool check(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m)        return true;    return false;}int dfs(int flag,int x,int y,int step){    if(check(x,y)==0)    return 0;    if(map[x][y]=='#')    return 0;    if(map[x][y]=='E')    return step+1;    if(flag)    {        dir=(dir+3)%4;        while(1)        {            int tx,ty;            tx =x+lx[dir];            ty =y+ly[dir];            ans=dfs(1,tx,ty,step+1);            if(ans)            break;            dir=(dir+1)%4;//不能搜了立马换方向去搜        }    }    else    {        dir=(dir+3)%4;        while(1)        {            int tx,ty;            tx=x+rx[dir];            ty=y+ry[dir];            ans=dfs(0,tx,ty,step+1);            if(ans>0)            break;            dir=(dir+1)%4;        }    }    return ans;}void bfs(){    node a,b;    queue<node> q;    int vis[45][45]={0};    a.x=sx;    a.y=sy;    a.step=1;    vis[a.x][a.y]=1;    q.push(a);    while(!q.empty())    {        a=q.front();        q.pop();        for(int i=0;i<4;i++)        {            b.x=a.x+lx[i];            b.y=a.y+ly[i];            b.step=a.step+1;            if(check(b.x,b.y)&&vis[b.x][b.y]==0)            {               if(map[b.x][b.y]=='E')               {                   cout<<b.step<<endl;                   return ;               }               vis[b.x][b.y]=1;                if(map[b.x][b.y]!='#')               q.push(b);            }        }    }}int main(){    int lsum,rsum,t;    cin>>t;    while(t--)    {        cin>>m>>n;        for(int i=0;i<n;i++)            cin>>map[i];           for(int i=0;i<n;i++)           {               for(int j=0;j<m;j++)               {                   if(map[i][j]=='S')                   {                       sx=i;                       sy=j;                   }                if(map[i][j]=='E')                  {                      ex=i;                      ey=j;                  }               }           }        dir=0;        lsum=dfs(1,sx,sy,0);        dir=0;        rsum=dfs(0,sx,sy,0);        cout<<lsum<<" "<<rsum<<" ";        bfs();    }    return 0;}





0 0