zoukankan      html  css  js  c++  java
  • poj3083——dfs+bfs综合题

    POJ 3083   dfs+bfs+模拟

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

    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

    题意:从迷宫起点S到终点E,分别求沿左边的墙走的步数,沿右边墙走的步数,最短路的步数;
    思路:最短路直接bfs,沿着墙走dfs+模拟,每次递归时记录方向和并更改计算下一时刻的方向,按方向顺时针(或逆时针)走,注意取余;
    难点:模拟。。。弄了3个多小时。。。
    bfs还忘了改vis判重造成MLE。。。。模拟时还把左右方向弄反了。。。
    唯一欣慰的是0秒过了。。。
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<vector>
    #include<queue>
    
    using namespace std;
    
    const int maxn=43;
    
    int n,m;
    char ch[maxn][maxn];
    bool vis[maxn][maxn];
    int si,sj;
    int ei,ej;
    int stepL,stepR,stepS;
    struct node
    {
        int x,y;
    };
    int dx[]={0,1,0,-1};
    int dy[]={-1,0,1,0};
    bool flag;
    
    
    
    void bfs(int si,int sj)
    {
        queue<node> q;
        q.push({si,sj});
        int dist[maxn][maxn];
        memset(dist,0,sizeof(dist));
        dist[si][sj]=1;
        vis[si][sj]=1;
        while(!q.empty()){
            node now=q.front();
            q.pop();
            for(int i=0;i<4;i++){
                int nx=now.x+dx[i],ny=now.y+dy[i];
                if(vis[nx][ny]||ch[nx][ny]=='#') continue;
                vis[nx][ny]=1;
                dist[nx][ny]=dist[now.x][now.y]+1;
                if(ch[nx][ny]=='E'){
                    stepS=dist[nx][ny];
                    return;
                }
                q.push({nx,ny});
            }
        }
    }
    
    void dfs(int x,int y,char end,int dir,int step,bool tag)
    {
        if(flag) return;
        if(ch[x][y]==end){
            if(tag) stepL=step;
            else stepR=step;
            flag=1;return;
        }
        for(int i=0;i<4;i++){
            int nx=x+dx[(dir+i)%4],ny=y+dy[(dir+i)%4];
            int ndir=(dir+i+3)%4;
            if(ch[nx][ny]=='#') continue;
            dfs(nx,ny,end,ndir,step+1,tag);
        }
    }
    
    int main()
    {
        int T;cin>>T;
        while(T--){
            cin>>m>>n;
            stepL=stepR=stepS=0;
            memset(ch,'#',sizeof(ch));
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    cin>>ch[i][j];
                    if(ch[i][j]=='S'){
                        si=i;sj=j;
                    }
                    if(ch[i][j]=='E'){
                        ei=i;ej=j;
                    }
                }
            }
            flag=0;
            dfs(si,sj,'E',0,1,1);
            flag=0;
            dfs(ei,ej,'S',0,1,0);
            memset(vis,0,sizeof(vis));
            bfs(si,sj);
            cout<<stepR<<" "<<stepL<<" "<<stepS<<endl;
        }
        return 0;
    }
    poj3083
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    MyBatis的初始化方式
    WCF X.509验证
    NPOI导出EXCEL 打印设置分页及打印标题
    一些旁门左道
    曲线救国:IIS7集成模式下如何获取网站的URL
    图片的粘贴上传
    让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
    ASP.NET 保存txt文件
    JavaScript高级程序设计学习笔记--高级技巧
    JavaScript高级程序设计学习笔记--错误处理与调试
  • 原文地址:https://www.cnblogs.com/--560/p/4331273.html
Copyright © 2011-2022 走看看