问题 1672: 迷宫问题 (BFS)
题目链接:https://www.dotcpp.com/oj/problem1672.html
问题 1672: 迷宫问题
时间限制: 1Sec 内存限制: 32MB 提交: 663 解决: 158
小明只能向上下左右四个方向移动。
每组输入的第一行是两个整数N和M(1<=N,M<=100)。
接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格。
字符的含义如下:
‘S’:起点
‘E’:终点
‘-’:空地,可以通过
‘#’:障碍,无法通过
输入数据保证有且仅有一个起点和终点。
1
5 5
S-###
-----
##---
E#---
---##
9
宽度优先搜索的常规题,但是需要注意判断:如果不存在通路需要返回-1(否则只能过50%,存在一半的数据);
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <cstdio>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;
char maze[][];
int N,M;
int sx,sy;
int gx,gy;
int d[][];
int dx[]={,-,,},dy[]={,,,-};
int t;
int bfs()
{
queue<P> que;
for(int i=;i<N;i++){
for(int j=;j<M;j++){
d[i][j]=INF;
}
}
que.push(P(sx,sy));
d[sx][sy]=;
while(que.size()){
P p=que.front();
que.pop();
if(p.first==gx&&p.second==gy) break;
for(int i=;i<;i++){
int nx=p.first+dx[i],ny=p.second+dy[i];
if(nx>=&&nx<N&&ny>=&&ny<M&&maze[nx][ny]!='#'&&d[nx][ny]==INF){
que.push(P(nx,ny));
d[nx][ny]=d[p.first][p.second]+;
}
}
}
if(d[gx][gy]==INF) return -;
else return d[gx][gy];
}
int main()
{
while(cin>>t){
while(t--){
cin>>N>>M;
for(int i=;i<N;i++){
for(int j=;j<M;j++){
cin>>maze[i][j];
if(maze[i][j]=='S'){
sx=i;
sy=j;
}
if(maze[i][j]=='E'){
gx=i;
gy=j;
}
}
}
cout<<bfs()<<endl;
}
}
return ;
}
。。。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;
int t,n;
int N,M;
int d[][];
char a[][];
int dx[]={,-,,};
int dy[]={,,,-};
int sx,sy,gx,gy;
int x,y,nx,ny;
int bfs()
{
memset(d,INF,sizeof(d));
queue<P> que;
que.push(P(sx,sy));
d[sx][sy]=;
while(!que.empty()){
P p=que.front();
que.pop();
x=p.first,y=p.second;
if(x==gx&&y==gy) break;
for(int i=;i<;i++){
nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<N&&ny>=&&ny<M&&a[nx][ny]!='#'&&d[nx][ny]==INF){
que.push(P(nx,ny));
d[nx][ny]=d[x][y]+;
}
}
}
if(d[gx][gy]==INF) return -;
return d[gx][gy];
}
int main()
{
while(cin>>t){
while(t--){
cin>>N>>M;
for(int i=;i<N;i++){
for(int j=;j<M;j++){
cin>>a[i][j];
if(a[i][j]=='S'){
sx=i,sy=j;
}else if(a[i][j]=='E'){
gx=i,gy=j;
}
}
}
cout<<bfs()<<endl;
}
}
return ;
}
问题 1672: 迷宫问题 (BFS)的更多相关文章
- 迷宫问题(bfs)
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class BFS { priv ...
- ZZULIOJ 1726 迷宫(BFS+小坑)
1726: 迷宫 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 394 Solved: 64 SubmitStatusWeb Board Descr ...
- HDU 1728 逃离迷宫(BFS)
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- 迷宫问题(bfs的应用)
问题描述: 定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, ...
- [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)
题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)
题目链接:https://www.dotcpp.com/oj/problem1923.html 题目描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
随机推荐
- __x__(17)0906第三天__块元素block_内联元素inline_行内块元素inline-block
1. 块元素block 独占一行的元素 一般使用块元素包含内联元素,用作页面布局 <a> 标签可以包含任何除了a标签以外的元素 <p> 标签不能包含块元素 h1... ...h ...
- Linux搭建git服务端
1.安装$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel$ yum inst ...
- python 中的类
1.1 类里面包括:类的属性.方法 OO的特征(Object Oriented) 封装:信息隐蔽技术 继承:继承是子类自动共享父类之间数据和方法的机制 >>> class MyCla ...
- android 自定义Button,抛弃写shape文件
标签: android 控件 自定义 2017年05月27日 17:52:13 611人阅读 评论(0) 收藏 举报 分类: 自定义View(2) 作者同类文章 X 版权声明:本文为博主原创文章 ...
- Nginx负载均衡后端健康检查(支持HTTP和TCP)
之前有一篇文章记录nginx负载均衡后端检查,链接为 https://www.cnblogs.com/minseo/p/9511456.html 但是只包含http健康检查不包含tcp下面安装ngin ...
- 电子产品使用感受之——为什么我把Apple Watch S2 升级到了 S4?
2019.03.14 更新 最近在手表上安装了“摩拜单车”的APP,这绝对是一款使用体验加分的APP. 我每天上下班都要骑摩拜单车,但是每次掏出手机,首先FACE ID解锁屏幕,然后从上往下滑屏幕来触 ...
- java中String常量的存储原理
相关题目(运行结果在代码注释后面) 1. package StringTest; public class test1 { public static void main(String[] args) ...
- 刀客139qq算命
https://www.zhouyi.cc/bazi/sm/BaZi.php 好的算命网站 根据辛亥时看出来的午亥暗合没那么简单他不克你她太弱了婚姻不顺,是因为夫妻宫是财坏印是要比劫克财克财很重要 出 ...
- Elasticsearch学习笔记(五)索引元数据和集群元数据
一.索引元数据 执行:GET /ecommerce/product/1 返回结果: { "_index": "ecommerce", "_ty ...
- 使用STS创建springboot项目pom.xml文件报错org.apache.maven.archiver.MavenArchiver.getManifest
首先我的STS版本时:3.7.3 解决办法:->help->Install New Software -> add->location ->输入: http://repo ...