zoukankan      html  css  js  c++  java
  • BFS

    Children of the Candy Corn:

    题目链接:

    http://poj.org/problem?id=3083

    题目:

    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

    思路:

    题目的意思是给一个地图输出沿着左墙壁走的步数,沿着有墙壁走的步数,以及最短路的步数

    写三个bfs就可以实现

    沿着左墙壁走时  当朝向前方的时候 走的优先顺序是  左 前 右 后

    沿着右墙壁走时  当朝向前方的时候 走的优先顺序是  右 前 左 后

    所以定义一个所走的朝向 分别写两个bfs就可以实现了  

    代码:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    using namespace std;
    int t,n,m,vis[50][50],bx,by,ex,ey;
    char pic[50][50];
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    int ldx[8]={0,-1,0,1,0,-1,0,1};
    int ldy[8]={-1,0,1,0,-1,0,1,0};
    int rdx[8]={1,0,-1,0,1,0,-1,0};
    int rdy[8]={0,1,0,-1,0,1,0,-1};
    struct node
    {
       int x,y,step,chaoxiang;
    };
     
    void lbfs()//沿左墙壁走
    {
        queue<node>q;
        node b,now,next;
        b.x=bx;
        b.y=by;
        b.step=1;
        b.chaoxiang=1;//先把初始的朝向定义为1,任何数字都可以 
        q.push(b);    //因为可以重复走就不用定义标记数组
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(pic[now.x][now.y]=='E')
            {
                printf("%d ",now.step);
                return;
            }
            for(int d=((now.chaoxiang+3)%4);d<((now.chaoxiang+3)%4+4);d++)//每次首先走的都是朝向的左边
            {
                next=now;
                next.x+=ldx[d];
                next.y+=ldy[d];
                next.step++;
                if(next.x<0||next.y<0||next.x>=n||next.y>=m||pic[next.x][next.y]=='#'||pic[next.x][next.y]=='S')continue;
                else
                {
                    if(d>=4)next.chaoxiang=d-4;
                    else next.chaoxiang=d;//给定现在的朝向
                    q.push(next);
                    break;
                }
            }
        }
    }
    void rbfs()
    {
        queue<node>q;
        node b,now,next;
        b.x=bx;
        b.y=by;
        b.step=1;
        b.chaoxiang=1;
        q.push(b);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(pic[now.x][now.y]=='E')
            {
                printf("%d ",now.step);
                return;
            }
            for(int d=((now.chaoxiang+3)%4);d<((now.chaoxiang+3)%4+4);d++)
            {
                next=now;
                next.x+=rdx[d];
                next.y+=rdy[d];
                next.step++;
                if(next.x<0||next.y<0||next.x>=n||next.y>=m||pic[next.x][next.y]=='#'||pic[next.x][next.y]=='S')continue;
                else
                {
                    if(d>=4)next.chaoxiang=d-4;
                    else next.chaoxiang=d;
                    q.push(next);
                    break;
                }
            }
        }
    }
    void bfs()
    {
        queue<node>q;
        node b,now,next;
        b.x=bx;
        b.y=by;
        b.step=1;
        vis[bx][by]=1;
        q.push(b);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(now.x==ex&&now.y==ey)
            {
                printf("%d
    ",now.step);
                return;
            }
            for(int d=0;d<4;d++)
            {
                next=now;
                next.x+=dx[d];
                next.y+=dy[d];
                next.step++;
                if(next.x<0||next.x>=n||next.y<0||next.y>=m)continue;
                if(pic[next.x][next.y]=='#')continue;
                if(vis[next.x][next.y]==0)
                {
                    vis[next.x][next.y]=1;
                    q.push(next);
                }
            }
        }
     
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
        scanf("%d",&t);
        for(int i=0; i<t; i++)
        {
            scanf("%d%d",&m,&n);
            getchar();
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    scanf("%c",&pic[i][j]);
                    if(pic[i][j]=='S'){bx=i;by=j;}
                    if(pic[i][j]=='E'){ex=i;ey=j;}
                }
                getchar();
            }
            lbfs();
            rbfs();
            memset(vis,0,sizeof(vis));
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    update语句更新多条记录, 标记下
    点击超链接从VSTF、SVN及文件共享服务器上下载文件
    批量插入数据, 将DataTable里的数据批量写入数据库的方法
    SqlServer规范, 标记下
    学习笔记Mysql操作总结
    今拾到了个联合查询, 琢磨了好几个小时, 标记一下
    【读书笔记】 拜读潘加宇的《软件方法》一书的摘抄(上)
    关于引用类型的 ref 传参操作
    sed 正则表达式处理日志
    各省市个税计算器
  • 原文地址:https://www.cnblogs.com/20172674xi/p/9545738.html
Copyright © 2011-2022 走看看