问题 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__(38)0909第五天__雪碧图的制作
1. 用ps打开目标图片若干. 2. 调整合适的画布大小. 3. 将图片拖曳到一张里. 4. 存储为Web所用格式,选择 png24 .
- TextInputLayout 用法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- [Codeforces Round #433][Codeforces 853C/854E. Boredom]
题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...
- java学习之路--I/O流
java基础学习总结——流 一.JAVA流式输入/输出原理
- cadence单一原理图库的设计
- 关于eclipse的Progress一直跳转的解决方案
下载eclipse编程,发现了一个问题:执行main方法第二次console打印不出数据,后发现Progress一直跳转,而且非常多进度条在运行,关闭后第一次执行没问题,第二次问题重复出现. 有幸看到 ...
- 织梦手机站下一篇变上一篇而且还出错Request Error!
最新的织梦dedecms程序手机版下一篇变上一篇而且还出错Request Error!,这是因为官方写错了一个地方 打开 /include/arc.archives.class.php 找到 $mli ...
- windows创建定时任务执行python脚本
一.创建定时任务 \ [程序或脚本]文本框中填的是Python编译器的名称,一般就是python.exe, [起始于]文本框中填的是Python编译器的目录,上图中假设你的Python编译器的完整路径 ...
- en-zh(科学技术)science and technology-2
研究:长期不吃早餐,患心脏病风险增加87% Skipping breakfast could raise risk of heart disease by 87% Skipping breakfast ...
- JavaScript关于md5加密
/*中文加密 *181009 * */ function md5(string) { var x = Array(); var k, AA, BB, CC, DD, a, b, c, d; var S ...