• 【POJ 3083】Children of the Candy Corn


    Children of the Candy Corn
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11939   Accepted: 5153

    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



    题目大意:求迷宫中1.只左转到达终点,2.只右转到达终点,3.最短路径。题目保证可以到达终点。

    思路:2次dfs+1次bfs。其中bfs很简单,dfs只需要加一个face变量表示朝向,根据朝向进行dfs即可。


    代码:

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <queue>
    using namespace std;
    struct Node
    {
        int x,y,step;
    } temp,now;
    int movement[4][2]= {{0,1},{-1,0},{0,-1},{1,0}};
    bool flag;
    queue<Node>q;
    int h,w,startx,starty,ans1,ans2;
    char map[50][50];
    int vis[50][50];
    bool check(int x,int y)
    {
        if(x>=0&&x<w&&y>=0&&y<h&&!vis[x][y]&&map[x][y]!='#')
            return true;
        return false;
    }
    void dfs1(int x,int y,int step,int face)
    {
        if(flag)
            return;
        if(map[x][y]=='E')
        {
            flag=true;
            ans1=step;
            return;
        }
        if(face==1)
        {
            if(check(x+1,y))    dfs1(x+1,y,step+1,4);
            if(check(x,y-1))    dfs1(x,y-1,step+1,1);
            if(check(x-1,y))    dfs1(x-1,y,step+1,2);
            if(check(x,y+1))    dfs1(x,y+1,step+1,3);
        }
        else if(face==2)
        {
            if(check(x,y-1))    dfs1(x,y-1,step+1,1);
            if(check(x-1,y))    dfs1(x-1,y,step+1,2);
            if(check(x,y+1))    dfs1(x,y+1,step+1,3);
            if(check(x+1,y))    dfs1(x+1,y,step+1,4);
        }
        else if(face==3)
        {
            if(check(x-1,y))    dfs1(x-1,y,step+1,2);
            if(check(x,y+1))    dfs1(x,y+1,step+1,3);
            if(check(x+1,y))    dfs1(x+1,y,step+1,4);
            if(check(x,y-1))    dfs1(x,y-1,step+1,1);
        }
        else
        {
            if(check(x,y+1))    dfs1(x,y+1,step+1,3);
            if(check(x+1,y))    dfs1(x+1,y,step+1,4);
            if(check(x,y-1))    dfs1(x,y-1,step+1,1);
            if(check(x-1,y))    dfs1(x-1,y,step+1,2);
        }
    }
    void dfs2(int x,int y,int step,int face)
    {
        if(flag)
            return;
        if(map[x][y]=='E')
        {
            flag=true;
            ans2=step;
            return;
        }
        if(face==1)
        {
            if(check(x-1,y))     dfs2(x-1,y,step+1,2);
            if(check(x,y-1))     dfs2(x,y-1,step+1,1);
            if(check(x+1,y))     dfs2(x+1,y,step+1,4);
            if(check(x,y+1))     dfs2(x,y+1,step+1,3);
        }
        else if(face==2)
        {
            if(check(x,y+1))     dfs2(x,y+1,step+1,3);
            if(check(x-1,y))     dfs2(x-1,y,step+1,2);
            if(check(x,y-1))     dfs2(x,y-1,step+1,1);
            if(check(x+1,y))     dfs2(x+1,y,step+1,4);
        }
        else if(face==3)
        {
            if(check(x+1,y))     dfs2(x+1,y,step+1,4);
            if(check(x,y+1))     dfs2(x,y+1,step+1,3);
            if(check(x-1,y))     dfs2(x-1,y,step+1,2);
            if(check(x,y-1))     dfs2(x,y-1,step+1,1);
        }
        else
        {
            if(check(x,y-1))     dfs2(x,y-1,step+1,1);
            if(check(x+1,y))     dfs2(x+1,y,step+1,4);
            if(check(x,y+1))     dfs2(x,y+1,step+1,3);
            if(check(x-1,y))     dfs2(x-1,y,step+1,2);
        }
    }
    int bfs(int a,int b)
    {
        temp.x=a;
        temp.y=b;
        temp.step=1;
        q.push(temp);
        vis[a][b]=1;
        while(!q.empty())
        {
            temp=q.front();
            q.pop();
            if(map[temp.x][temp.y]=='E')
                return temp.step;
            for(int i=0; i<4; i++)
            {
                now.x=temp.x+movement[i][0];
                now.y=temp.y+movement[i][1];
                if(check(now.x,now.y))
                {
                    vis[now.x][now.y]=1;
                    now.step=temp.step+1;
                    //cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
                    q.push(now);
                }
            }
        }
        return -1;
    }
    int main()
    {
        int t;
        while(~scanf("%d",&t))
        {
            while(t--)
            {
                while(!q.empty())
                    q.pop();
                scanf("%d%d",&h,&w);
                for(int i=0; i<w; i++)
                    scanf("%s",map[i]);
                for(int i=0; i<w; i++)
                {
                    for(int j=0; j<h; j++)
                    {
                        if(map[i][j]=='S')
                        {
                            startx=i;
                            starty=j;
                        }
                    }
                }
                ans1=ans2=0;
                memset(vis,0,sizeof(vis));
                flag=false;
                dfs1(startx,starty,1,1);
                flag=false;
                dfs2(startx,starty,1,1);
                memset(vis,0,sizeof(vis));
                int minn=bfs(startx,starty);
                printf("%d %d %d
    ",ans1,ans2,minn);
            }
        }
    }
    



  • 相关阅读:
    修改sql表操作大全
    Asp.NET自定义DataGrid控件
    jQuery LigerUI 插件介绍及使用之ligerGrid
    jQuery LigerUI API预览版发布
    jQuery LigerUI 初次发布&一睹为快(提供Demo下载)
    jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable
    nginx 日志
    nginx 正向代理
    排序
    nginx 静态资源优化
  • 原文地址:https://www.cnblogs.com/Torrance/p/5410565.html
Copyright © 2020-2023  润新知