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());
}												
											bearBaby loves sleeping(BFS)的更多相关文章
- B	bearBaby loves sleeping
		
链接:https://ac.nowcoder.com/acm/contest/338/B来源:牛客网 题目描述 Sleeping is a favorite of little bearBaby, b ...
 - nyoj 21三个水杯(BFS + 栈)
		
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...
 - POJ3279 Catch That Cow(BFS)
		
本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...
 - 深搜(DFS)广搜(BFS)详解
		
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
 - 【算法导论】图的广度优先搜索遍历(BFS)
		
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
 - 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
		
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
 - 【BZOJ5492】[HNOI2019]校园旅行(bfs)
		
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
 - 【Luogu3602】Koishi Loves Segments(贪心)
		
[Luogu3602]Koishi Loves Segments(贪心) 题面 洛谷 题解 离散区间之后把所有的线段挂在左端点上,从左往右扫一遍. 对于当前点的限制如果不满足显然会删掉右端点最靠右的那 ...
 - 【BZOJ3309】DZY Loves Math(莫比乌斯反演)
		
[BZOJ3309]DZY Loves Math(莫比乌斯反演) 题面 求 \[\sum_{i=1}^a\sum_{j=1}^bf(gcd(a,b))\] 其中,\(f(x)\)表示\(x\)分解质因 ...
 
随机推荐
- IDEAL葵花宝典:java代码开发规范插件:GsonFormat插件将JSONObject格式的String 解析成实体
			
前言: GsonFormat插件主要用于使用Gson库将JSONObject格式的String 解析成实体,该插件可以加快开发进度,使用非常方便,效率高. 这个教程主要是学习IntelliJ IDEA ...
 - linux svn yum 安装、开机自启动
			
1.查询是否安装 rpm -qa subversion
 - 发挥到极致的Asterisk SS7 解决方案【转】
			
基于SS7的开源解决方案在国内已经安装了很多.很多用户都使用chan_ss7 开源协议栈作为呼叫中心,400电话,计费结算的系统.随着国内对开源Asterisk的认可程度越来越高. Asterisk让 ...
 - [HAOI 2011] Problem A
			
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2298 [算法] 考虑用总人数 - 最多人说真话 显然 , 对于每个人 , 如果他说的 ...
 - bzoj 5093 图的价值 —— 第二类斯特林数+NTT
			
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5093 每个点都是等价的,从点的贡献来看,得到式子: \( ans = n * \sum\li ...
 - 基于Html5的移动端APP开发框架
			
快速增长的APP应用软件市场,以及智能手机的普及,手机应用:Native(原生)APP快速占领了APP市场,成为了APP开发的主流,但其平台的不通用性,开发成本高,多版本开发等问题,一直困扰着专业AP ...
 - APNS消息推送实现
			
转自:http://blog.csdn.net/biaobiaoqi/article/details/8058503 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1 ...
 - java:Socket介绍(一)
			
Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单 ...
 - [hdu2087]剪花布条(KMP)
			
题意:求存在模式串个数,不可重复. 解题关键:模板题.整理模板用.重复和不可重复的区别在下面已标出.主要是j的变化. #include<cstdio> #include<cstrin ...
 - 5.zip隐写术
			
首先分析pcapng,来得到flag. 先亮出下载地址:http://ctf5.shiyanbar.com/misc/LOL/LOL.pcapng 由于这是.pcapng,需要下载 wireshark ...