链接:https://ac.nowcoder.com/acm/contest/338/B
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.

链接:https://ac.nowcoder.com/acm/contest/338/B
来源:牛客网

输入描述:

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)

AC代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=,f=;char c=getchar();while(c!='-'&&(c<''||c>''))c=getchar();if(c=='-')f=-,c=getchar();while(c>=''&&c<='')x=x*+c-'',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e4+;
struct node{
int x,y;
int st;
}; int dir[][]={-,,,,,-,,};
int vis[maxn][maxn];
int n,m,n1,m1;
char a[][]; int bfs(int u,int v){
node tmp,nex;
tmp.x=u;
tmp.y=v;
tmp.st=;
queue<node>q;
q.push(tmp);
while(!q.empty()){
tmp=q.front();
q.pop();
for(int i=;i<;i++){
nex.x=tmp.x+dir[i][];
nex.y=tmp.y+dir[i][];
nex.st=tmp.st+;
if(nex.x<||nex.y<||nex.x>=n||nex.y>=m||a[nex.x][nex.y]==''||vis[nex.x][nex.y]==)continue;
vis[nex.x][nex.y]=;
if(nex.x==n1-&&nex.y==m1-){
return nex.st;
} q.push(nex);
}
}
}
int main()
{ cin>>n>>m>>n1>>m1;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
int ans=bfs(,);
printf("%d",ans);
return ;
}

bfs迷宫的更多相关文章

  1. bfs—迷宫问题—poj3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20591   Accepted: 12050 http://poj ...

  2. uva 816 - Abbott&#39;s Revenge(有点困难bfs迷宫称号)

    是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...

  3. BFS迷宫搜索路径

    #include<graphics.h> #include<stdlib.h> #include<conio.h> #include<time.h> # ...

  4. HDU2579(bfs迷宫)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. BFS迷宫问题

    链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...

  6. 【OpenJ_Bailian - 2790】迷宫(bfs)

    -->迷宫  Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...

  7. ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)

    题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...

  8. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  9. 3299: [USACO2011 Open]Corn Maze玉米迷宫

    3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[ ...

随机推荐

  1. [HEOI2015] 小Z的房间 - 矩阵树定理

    #include <bits/stdc++.h> using namespace std; #define int long long const int N = 105; const i ...

  2. IIS新类型文件500错误

    时间过得太久,就真的会忘掉.在这里记录一下吧. 不得已重新安装系统,然后以前的配置都忘掉了.对新类型的文件.geojson 文件报错. 500错误. 首先反复调试MIME类型是否有问题, 再看映射是否 ...

  3. 第四篇,JavaScript面试题汇总

    JavaScript是一种属于网络的脚本语言,已经被广泛用于web实用开发,常用来为网页添加各种各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HTML中来实现 ...

  4. foreach中如何取全部长度的值

    foreach($data as $num=>$key ){ return $num; } 关键就是这个$num;

  5. 【玩转SpringBoot】SpringBoot应用的启动过程一览表

    SpringBoot应用的启动方式很简单,就一行代码,如下图01: 其实这行代码背后主要执行两个方法,一个是构造方法,一个是run方法. 构造方法主要内容就是收集一些数据,和确认一些信息.如下图02: ...

  6. 浅谈ABB机器人(工具坐标,工件坐标,有效载荷)

    工具坐标(tool): 使tcl坐标偏移到工具上,例如焊接工作,使机器人工作点切入焊枪点上 mass:工具的重量 xyz:偏移距离的大小 验证:通过手动模式,切换至自定义工具,重定向 工件坐标(wob ...

  7. kali 安装google输入法

    脑子一热装了一礼拜的kali,在20多遍的重装后终于成功了 先码一篇如何安装google输入法 首先得更新源,用leafpad /etc/apt/sources.list打开,或vi也可以,更新源百度 ...

  8. windows系统下,gpu开发环境部署

    1,安装python,使用anaconda或者直接用python.exe安装都可以.我用的是python3.6版的 对于相关的程序包,比如tensorflow或者opencv等,anaconda可以在 ...

  9. iview修改table组件实现循环向上滚屏

    前提,最近项目中需要实现table的滚屏效果,并且使用的是iview的table组件,踩坑,填坑如下. 1.首先找到Table组件中的table,就是这个class:ivu-table-body te ...

  10. k8s 部署 custom-metrics-apiserver 时使用 secret 保存 ca 证书遇到的问题

    部署 k8s-prometheus-adapter 的 custom-metrics-apiserver 时,pod 总是启动失败,对应的错误日志: unable to install resourc ...