zoukankan      html  css  js  c++  java
  • 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());
    }
  • 相关阅读:
    table常用功能总结
    oracle中number类型的数据使用as string 得到的值为null
    Oracle连接超时
    VS重新生成后仍然执行旧代码
    .NET 环境中使用RabbitMQ
    使用CLR Function代替T-SQL函数,优化检索效率
    SQL Server CLR 使用 C# 自定义存储过程和触发器
    OrderBy排序和IComparer的使用
    System.net.mail 使用ssl发送邮件失败
    C#利用CDO.Message发送邮件
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781857.html
Copyright © 2011-2022 走看看