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\n",bfs());
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值