题目链接:https://www.dotcpp.com/oj/problem1672.html

问题 1672: 迷宫问题

时间限制: 1Sec 内存限制: 32MB 提交: 663 解决: 158

题目描述
小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程。
小明只能向上下左右四个方向移动。
输入
输入包含多组测试数据。输入的第一行是一个整数T,表示有T组测试数据。
每组输入的第一行是两个整数N和M(1<=N,M<=100)。
接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格。
字符的含义如下:
‘S’:起点
‘E’:终点
‘-’:空地,可以通过
‘#’:障碍,无法通过
输入数据保证有且仅有一个起点和终点。
输出
对于每组输入,输出从起点到终点的最短路程,如果不存在从起点到终点的路,则输出-1。
样例输入
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)的更多相关文章

  1. 迷宫问题(bfs)

    import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class BFS { priv ...

  2. ZZULIOJ 1726 迷宫(BFS+小坑)

    1726: 迷宫 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 394  Solved: 64 SubmitStatusWeb Board Descr ...

  3. HDU 1728 逃离迷宫(BFS)

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...

  4. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  5. 迷宫问题(bfs的应用)

    问题描述: 定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, ...

  6. [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)

    题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  7. 问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)

    题目链接:https://www.dotcpp.com/oj/problem1923.html 题目描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在 ...

  8. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  9. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

随机推荐

  1. Array,prototype.concat.apply与[].conat.apply.

    一直都知道JS数组Array内置对象有一个concat方法,但是也没怎么研究过,今天偶然就看了看 concat是连接一个或多个数组 返回的是连接后数组的一个副本 var oldArr=[]; var ...

  2. 隐藏用户建立(Powershell)

    最近做测试的时候发现,windows server2012 使用Mimikatz是直接抓不到明文密码的,而且,直接创建的账号登陆有时会碰到这个问题: ps:2012抓明文需要HKLM:\SYSTEM\ ...

  3. word/pdf互转的链接

    参考链接 http://www.greenxf.com/soft/98467.html

  4. linux安装杀毒软件

    https://www.cnblogs.com/bingo1024/p/9018212.html

  5. 2018-2019-2 网络对抗技术 20165311 Exp6 信息搜集与漏洞扫描

    20165311 Exp6 信息搜集与漏洞扫描 1.实验内容 2.实验过程 任务一:各种搜索技巧的应用 通过搜索引擎进行信息搜集 使用FOFA.SO 搜索特定类型的文件 任务二:DNS IP注册信息的 ...

  6. Jmeter-----参数配置

    参数化配置: 设置为3个线程后,三个用户均能运行

  7. CSCI 1100 — Computer Science 1 Homework

    CSCI 1100 — Computer Science 1 Homework 8CS1 Multiverse: ClassesOverviewThis homework is worth 100 p ...

  8. angular脚手架搭建

    下面以angular2.0为例前提已安装好node.js 1.安装cli执行如下命令npm install -g @angular/cli 2.创建新项目ng new my-app 3.然后到该项目目 ...

  9. vue 使用

    [1_01 vue的双向绑定] [1_02 Vue Slot] [vue实例属性template不能使用] [vue-property-decorator 提供 OO 的风格 Vue Componen ...

  10. Redis Keys的通用操作

    keys * 显示所有key 127.0.0.1:6379> keys * 1) "sort1" 2) "l2" 3) "set2" ...