zoukankan      html  css  js  c++  java
  • poj3083

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

    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

    Source

    题意:

    先沿着左边的墙从 S 一直走,求到达 E 的步数。

    再沿着右边的墙从 S 一直走,求到达 E 的步数。

    最后求最短路。

    分析:

    最短路好办,关键是沿着墙走不太好想。

    但只要弄懂如何转,这题就容易了。

    单就沿着左走看一下:

    当前方向     检索顺序

         ↑ :      ← ↑ → ↓

        → :        ↑ → ↓ ← 

         ↓ :      → ↓ ← ↑ 

        ← :        ↓ ← ↑ → 

    如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

    在DFS时,加一个参数,用来保存当前的方向。

    后来我发现了c++与g++

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    const int maxn=100+10;
    int dx[]={0,-1,0,1};
    int dy[]={-1,0,1,0};
    int dl[][2]={{0,-1},{-1,0},{0,1},{1,0}};
    int dr[][2]={{0,1},{-1,0},{0,-1},{1,0}};
    int sx,sy,ex,ey,n,m;
    char g[maxn][maxn];
    struct node{
        int x,y,s;
        node (int x=0,int y=0,int s=0):x(x),y(y),s(s){}
    };
    int dfs(int x,int y,int d,int step,int dir[][2]) {
        for(int i=0;i<4;i++){
            int j=((d-1+4)%4+i)%4;
            int nx=x+dir[j][0];
            int ny=y+dir[j][1];
            if(nx==ex&&ny==ey) return step+1;
            if(nx<0||ny<0||nx>n||ny>m) continue;
            if(g[nx][ny]=='#') continue;
            return dfs(nx,ny,j,step+1,dir);
        }
    }
    int bfs(int sx,int sy) {
        bool vis[maxn][maxn];
        memset(vis,false,sizeof(vis));
        queue<node>que;
        que.push((node){sx,sy,1});
        vis[sx][sy]=true;
        while(!que.empty()) {
            node p=que.front();que.pop();
            if(p.x==ex&&p.y==ey) return p.s;
            node np;
            for(int d=0;d<4;d++) {
                np.x=p.x+dx[d];
                np.y=p.y+dy[d];
                np.s=p.s+1;
                if(np.x<0||np.x>n||np.y<0||np.y>m) continue;
                if(vis[np.x][np.y]) continue;
                if(g[np.x][np.y] !='#'){
                    vis[np.x][np.y]=true;
                    que.push(np);
                }
            }
        }
        return -1;
    }
    int main() 
    {
        int T,d1,d2;
        scanf("%d",&T);
        while(T--) {
            scanf("%d%d",&m,&n);
            for(int i=0;i<n;i++){
                scanf("%s",g[i]);
                for(int j=0;j<m;j++){
                    if(g[i][j]=='S'){sx=i;sy=j;}
                    else if(g[i][j]=='E'){ex=i;ey=j;}
                }
            }
            if(sx==0)        {d1=3;d2=3;}
            else if(sx==n-1) {d1=1;d2=1;}
            else if(sy==0)   {d1=2;d2=0;}
            else if(sy==m-1) {d1=0;d2=2;}
            printf("%d ",dfs(sx,sy,d1,1,dl));
            printf("%d ",dfs(sx,sy,d2,1,dr));
            printf("%d
    ",bfs(sx,sy));
        }
        return 0;
    }
  • 相关阅读:
    C# WM_NCMOUSELEAVE 消息触发
    C#常用集合的使用(转载)
    关于直播,所有的技术细节都在这里了(转载)
    C# Winform 窗体美化
    正则表达式符号全解析
    C#中List<T>转DataTable
    C#中的Queue集合
    C#中Stack集合
    智能信息处理
    Mysql
  • 原文地址:https://www.cnblogs.com/shenben/p/5628296.html
Copyright © 2011-2022 走看看