Children of the Candy Corn

来源:互联网 发布:打开xlsx的软件 编辑:程序博客网 时间:2024/04/30 18:04

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




//Memory Time
// 0为上,1为左,2为下,3为右;
#include<iostream>
#include<cstring>
using namespace std;
typedef class
{
    public:
        int r,c;
        int depth;
}SE;
SE s,e; //起止点
int Lstep;  //左边优先搜索 时从S到E的总步数
int Rstep;  //右边优先搜索 时从S到E的总步数
int shortstep;  //S到E的最少总步数;
bool maze[41][41]; //记录迷宫的“可行域”与“墙”
void DFS_LF(int i,int j,int d)    //左边优先搜索,i,j为当前点坐标,d为当前位置方向
{
    Lstep++;
    if(i==e.r && j==e.c)
        return;


    switch(d)
    {
        case 0:
            {
                if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                else if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                else if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                else if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                break;
            }
        case 1:
            {
                if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                else if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                else if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                else if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                break;
            }
        case 2:
            {
                if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                else if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                else if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                else if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                break;
            }
        case 3:
            {
                if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                else if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                else if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                else if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                break;
            }
    }


    return;
}


void DFS_RF(int i,int j,int d)    //右边优先搜索,i,j为当前点坐标,d为当前位置方向
{
    Rstep++;
    if(i==e.r && j==e.c)
        return;
    switch(d)
    {
        case 0:
            {
                if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                else if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                else if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                else if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                break;
            }
        case 1:
            {
                if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                else if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                else if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                else if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                break;
            }
        case 2:
            {
                if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                else if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                else if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                else if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                break;
            }
        case 3:
            {
                if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                else if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                else if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                else if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                break;
            }
    }
    return;
}


void BFS_MSS(int i,int j)  //最短路搜索,s为树的根,E为树的最深叶子求树的深度;
{
    bool vist[41][41]={false};
    SE queue[1600];
    int head,tail;
    queue[head=0].r=i;
    queue[tail=0].c=j;
    queue[tail++].depth=1;  //当前树深标记,这是寻找最短路的关键点
    vist[i][j]=true;
    while(head<tail)
    {
        SE x=queue[head++];
        if(x.r==e.r && x.c==e.c)
        {
            cout<<x.depth<<endl;//树的深度;
            return;
        }


        if(maze[x.r][x.c-1] && !vist[x.r][x.c-1])
        {
            vist[x.r][x.c-1]=true;
            queue[tail].r=x.r;
            queue[tail].c=x.c-1;
            queue[tail++].depth=x.depth+1;
        }
        if(maze[x.r-1][x.c] && !vist[x.r-1][x.c])
        {
            vist[x.r-1][x.c]=true;
            queue[tail].r=x.r-1;
            queue[tail].c=x.c;
            queue[tail++].depth=x.depth+1;
        }
        if(maze[x.r][x.c+1] && !vist[x.r][x.c+1])
        {
            vist[x.r][x.c+1]=true;
            queue[tail].r=x.r;
            queue[tail].c=x.c+1;
            queue[tail++].depth=x.depth+1;
        }
        if(maze[x.r+1][x.c] && !vist[x.r+1][x.c])
        {
            vist[x.r+1][x.c]=true;
            queue[tail].r=x.r+1;
            queue[tail].c=x.c;
            queue[tail++].depth=x.depth+1;
        }
    }
    return;
}
int main(int i,int j)
{
    int test;
    cin>>test;
    while(test--)
    {
        int direction;  //起点S的初始方向
        int w,h;  //size of maze
        cin>>w>>h;
        /*Initial*/


        Lstep=1;
        Rstep=1;
        memset(maze,false,sizeof(maze));


        /*Structure the Maze*/


        for(i=1;i<=h;i++)
            for(j=1;j<=w;j++)
            {
                char temp;
                cin>>temp;
                if(temp=='.')
                    maze[i][j]=true;//可行域;
                if(temp=='S')//起始位置;
                {
                    maze[i][j]=true;
                    s.r=i;//横坐标,
                    s.c=j;//纵坐标;
                    if(i==h)//最后一行;
                        direction=0;//方向为上;
                    else if(j==w)//最后一列;
                        direction=1;//方向为左;
                    else if(i==1)//第一行;
                        direction=2;//方向为下;
                    else if(j==1)//第一列;
                        direction=3;//方向为右;
                }
                if(temp=='E')//到达终点;
                {
                    maze[i][j]=true;
                    e.r=i;
                    e.c=j;
                }
            }


        /*Left First Search*/


        switch(direction)
        {
            case 0: {DFS_LF(s.r-1,s.c,0); break;}
            case 1: {DFS_LF(s.r,s.c-1,1); break;}
            case 2: {DFS_LF(s.r+1,s.c,2); break;}
            case 3: {DFS_LF(s.r,s.c+1,3); break;}
        }
        cout<<Lstep<<' ';


        /*Right First Search*/


        switch(direction)
        {
            case 0: {DFS_RF(s.r-1,s.c,0); break;}
            case 1: {DFS_RF(s.r,s.c-1,1); break;}
            case 2: {DFS_RF(s.r+1,s.c,2); break;}
            case 3: {DFS_RF(s.r,s.c+1,3); break;}
        }
        cout<<Rstep<<' ';


        /*Most Short Step Search*/
        BFS_MSS(s.r,s.c);
    }
    return 0;
}


0 0
原创粉丝点击