链接: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. [CF467C] George and Job - DP,前缀和

    简单dp + 前缀和 你谷这乱标难度的风气真是-- #include <bits/stdc++.h> using namespace std; #define int long long ...

  2. 【转】mathnet 使用方法介绍

    转载自:http://blog.csdn.net/c914620529/article/details/50393223 在C#中使用mathnet,需要利用using引入相关类 矩阵运算的相关类: ...

  3. codeforces 1283E New Year Parties (贪心)

    链接:https://codeforces.com/contest/1283/problem/E 题意: 有n个人住在一些房子里,有的人住在同一个房子里.每个人可以选择搬去他的房子左边那个房子或者右边 ...

  4. IntelliJ IDEA 2017.3尚硅谷-----生成 javadoc

    Locale:输入语言类型:zh_CN Other command line arguments:-encoding UTF-8 -charset UTF-8

  5. 第三十二篇 玩转数据结构——AVL树(AVL Tree)

          1.. 平衡二叉树 平衡二叉树要求,对于任意一个节点,左子树和右子树的高度差不能超过1. 平衡二叉树的高度和节点数量之间的关系也是O(logn) 为二叉树标注节点高度并计算平衡因子 AVL ...

  6. 每天进步一点点------FIR(一)

    在数字信号处理系统中,有限脉冲响应(finite impulse response,FIR)数字滤波器是一个非常重要的基本单元.近年来,由于FPGA具有高速度.高集成度和高可靠性的特点而得到快速发展. ...

  7. window10配置远程虚拟机window7上的mysql5.7数据源

    原文链接:http://www.xitongcheng.com/jiaocheng/win10_article_18644.html windows10系统用户想要在电脑中设置ODBC数据源,于是手动 ...

  8. 【转自】自定义InputFormat、OutputFormat

    转自:http://www.cnblogs.com/xiaolong1032/p/4529534.html 一:自定义实现InputFormat *数据源来自于内存*1.InputFormat是用于处 ...

  9. java把带小数点的字符串转换成int类型

    String number ="1.0000"; int num =Double.valueOf(number).intValue();//转换为Int类型

  10. 拓扑排序(poj 1094)

    前置知识:拓扑排序 详细注释都在代码里 //该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. //是典型的拓扑排序,但输出格式上确有三种形式: // 1.该字母序列有序,并依 ...