• bearBaby loves sleeping(BFS)


    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 131072K,其他语言262144K
    64bit IO Format: %lld

    题目描述

    Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby  asks you to help him figure out the minimum time it takes to reach the teaching building.
    The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he  can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.


    输入描述:

    The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the column
    Next there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching building
    Next is a map of n rows and m columns, 0  indicate a open space and 1  indicate a obstacles.

    输出描述:

    For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.

    示例1

    输入

    复制

    5 4
    4 3
    0 0 1 0
    0 0 0 0
    0 0 1 0
    0 1 0 0
    0 0 0 1

    输出

    复制

    7

    说明

    For the input example, you could go like this:
    (1,1)-->(1,2)-->(2,2)-->(2,3)-->(2,4)-->(3,4)-->(4,4)-->(4,3),so the minimum time is 7.

    备注:

    First grid in the upper left corner is(1,1)

    思路很简单,就是BFS

    代码:

    #include<stdio.h>
    #include<stdbool.h>
    bool vis[105][105]={0};
    int ma[105][105];
    int go[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
    int sx,sy,ex,ey,n,m;
    struct nmsl
    {
        int x,y,step;
    }q[900000],u,v;
    int bfs()
    {
        int head=0,tail=0;
        q[tail].x=1;
        q[tail].y=1;
        q[tail++].step=0;
        vis[sx][sy]=1;
        while(tail!=head)
        {
            u=q[head++];
            if(u.x==ex&&u.y==ey)
            {
                return u.step;
            }
            for(int i=0;i<4;i++)
            {
                v=u;
                v.x+=go[i][0];
                v.y+=go[i][1];
                if(ma[v.x][v.y]) continue;
                if(v.x<=0||v.x>n||v.y<=0||v.y>m) continue;
                if(vis[v.x][v.y]) continue;
                v.step+=1;
                q[tail++]=v;
                vis[v.x][v.y]=1;
            }
        }
        return -1;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        scanf("%d%d",&ex,&ey);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&ma[i][j]);
            }
        }
        printf("%d
    ",bfs());
    }
  • 相关阅读:
    《算法竞赛进阶指南》0x12 队列 POJ2259 Team Queue
    《算法竞赛进阶指南》0x11栈 单调栈求矩形面积 POJ2559
    《算法竞赛进阶指南》0x11 栈 求解中缀表达式
    19.职责链模式(Chain of Responsibility Pattern)
    16.观察者模式(Observer Pattern)
    17.解释器模式(Interpreter Pattern)
    15. 迭代器模式(Iterator Pattern)
    14.命令模式(Command Pattern)
    12.代理模式(Proxy Pattern)
    13.模板方法(Template Method)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781857.html
Copyright © 2020-2023  润新知