时间限制: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());
}

bearBaby loves sleeping(BFS)的更多相关文章

  1. B bearBaby loves sleeping

    链接:https://ac.nowcoder.com/acm/contest/338/B来源:牛客网 题目描述 Sleeping is a favorite of little bearBaby, b ...

  2. nyoj 21三个水杯(BFS + 栈)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...

  3. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  8. 【Luogu3602】Koishi Loves Segments(贪心)

    [Luogu3602]Koishi Loves Segments(贪心) 题面 洛谷 题解 离散区间之后把所有的线段挂在左端点上,从左往右扫一遍. 对于当前点的限制如果不满足显然会删掉右端点最靠右的那 ...

  9. 【BZOJ3309】DZY Loves Math(莫比乌斯反演)

    [BZOJ3309]DZY Loves Math(莫比乌斯反演) 题面 求 \[\sum_{i=1}^a\sum_{j=1}^bf(gcd(a,b))\] 其中,\(f(x)\)表示\(x\)分解质因 ...

随机推荐

  1. T56

    警方派人监视那个可疑人的住宅.The police put a watch on the suspect's house.他们利用自己的实践经验,设计了一台气冷柴油机.According their ...

  2. 第十五章-Web开发

    随着web的发展, 最开始使用的CS架构已经不适合web了, 现在web使用的架构是BS架构 如今大部分重量级的软件都以web形式提供了 web开发的四个阶段 1) 静态web页面 2) CGI: 静 ...

  3. android自定义控件(一) 官方文档的翻译

    构建自定义组件 Android中,你的应用程序程序与View类组件有着一种固定的联系,例如按钮(Button).文本框(TextView),可编辑文本框(EditText),列表框(ListView) ...

  4. HDU5875Function(单调队列)

    The shorter, the simpler. With this problem, you should be convinced of this truth.      You are giv ...

  5. MySQL活动期间订单满600元并且在活动日期之前超过30天没有下过单_20161030

    计算 活动期间订单满600元并且在活动日期之前超过30天没有下过单 首先拿到这个需求,首先需要明确活动日期区间 10.29-10.31,其次要取这个时间段内某天订单额最高的那天及订单额,再次需要判断这 ...

  6. HDU3974(dfs+线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. python基础知识-字符串

    字符串: 赋值方法 a = 'name' a = str('name') 字符串的方法: #!/usr/bin/env python class str(object): ""&q ...

  8. .NETFramework:Random

    ylbtech-.NETFramework:Random 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c ...

  9. ASP.NET Core依赖注入解读&使用Autofac替代实现【转载】

    ASP.NET Core依赖注入解读&使用Autofac替代实现 1. 前言 2. ASP.NET Core 中的DI方式 3. Autofac实现和自定义实现扩展方法 3.1 安装Autof ...

  10. QDUOJ 来自xjy的签到题(bfs+状压dp)

    来自xjy的签到题   Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...