bfs迷宫
链接: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.
备注:
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迷宫的更多相关文章
- bfs—迷宫问题—poj3984
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20591 Accepted: 12050 http://poj ...
- uva 816 - Abbott's Revenge(有点困难bfs迷宫称号)
是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...
- BFS迷宫搜索路径
#include<graphics.h> #include<stdlib.h> #include<conio.h> #include<time.h> # ...
- HDU2579(bfs迷宫)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- BFS迷宫问题
链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...
- 【OpenJ_Bailian - 2790】迷宫(bfs)
-->迷宫 Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...
- ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)
题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...
- (BFS)poj2935-Basic Wall Maze
题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...
- 3299: [USACO2011 Open]Corn Maze玉米迷宫
3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 Solved: 59[ ...
随机推荐
- EF CodeFirst数据注解特性详解
数据注解特性是.NET特性,可以在EF或者EF Core中,应用于实体类上或者属性上,以重写默认的约定规则. 在EF 6和EF Core中,数据注解特性包含在System.ComponentModel ...
- CentOS 7防火墙快速开放端口配置方法
CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,baidu之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用firewalld开 ...
- Entity Framework 简介
Entity Framework Entity Framework 的全称为 ADO.NET Entity Framework,简称 EF. 1.与 ADO.NET 的关系 Entity F ...
- 使用 Razor 表达式
https://blog.csdn.net/github_37410569/article/details/54986136 https://blog.csdn.net/qq_21419015/art ...
- php设计模式之观察者模式实例代码
php提供的两个接口,一个被观察者接口SplSubject,一个或多个观察者接口SPLObserver,和一个可以储存对象的类SplObjectStorage.被观察者有三个方法,需要实现这三个方法, ...
- centos7 源码编译安装nginx教程 nginx安装脚本 lua-nginx-module
安装nginx需要pcre zlib openssl的库,下文都是在官网直接下载用作编译安装 该nginx安装教程,有安装maxmind IP 库 该nginx安装教程有安装lua-nginx-mod ...
- Go_MySQL查询插入删除
什么是预处理? 普通SQL语句执行过程: 客户端对SQL语句进行占位符替换得到完整的SQL语句. 客户端发送完整SQL语句到MySQL服务端 MySQL服务端执行完整的SQL语句并将结果返回给客户端. ...
- 用git无法连接github的解决方法
如果要從 GitHub 存取 Git 儲存庫,建議還是多採用 SSH 與 HTTPS 通訊協定最為穩定可靠,因此我的替代方案就是設定 Git 的全域設定值 ( –global ),預設將所有 git: ...
- 每天进步一点点------ORCAD Capture CIS
ORCAD Capture CIS 一.建工程及设置 1.选主菜单 file->new->project ;弹出 project wizard 对话框,取名Myproject : Mypr ...
- ThinkPhp5 中Volist标签的用法
Volist标签一般是和内置方法assign()搭配使用,将值从后台传到前台,是当下比较流行的一种传值方法 本文实例讲述了ThinkPHP模板循环输出Volist标签用法.分享给大家供大家参考,具体如 ...