博客
关于我
POJ - 3984 迷宫问题(bfs+路径标记)
阅读量:273 次
发布时间:2019-03-01

本文共 2965 字,大约阅读时间需要 9 分钟。

定义一个二维数组:

int maze[5][5] = {	0, 1, 0, 0, 0,	0, 1, 0, 1, 0,	0, 0, 0, 0, 0,	0, 1, 1, 1, 0,	0, 0, 0, 1, 0,};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0

Sample Output

(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)

我做了一周的搜索了  kuangbin专题还没全绿  yingyingying 我想口土

bfs+路径标记

NO1.

pair<int,int>path[20][20]来存路径,vis[20][20]像以前一样来标记该点有没有走过,其他没有什么特别的。

如果要输出步数的话可以把pair换成 结构体(本坐标、前驱坐标、步数)或者再开一个 step的二维数组 来存储每一点的步数。

#include 
#include
#include
#include
#include
using namespace std;int a,b,c;int dir[4][4]={ {1,0},{-1,0},{0,1},{0,-1}};bool vis[20][20];int mp[20][20];pair
path[20][20];void putout(int x,int y){ if(x==0&&y==0) return ; else { putout(path[x][y].first,path[x][y].second); printf("(%d, %d)\n",path[x][y].first,path[x][y].second); } if(x==4&&y==4) printf("(4, 4)\n");}void bfs(){ queue
>q; q.push(make_pair(0,0)); vis[0][0]=1; while(q.size()) { pair
n=q.front(); q.pop(); if(n.first==4&&n.second==4) return ; for(int i=0;i<4;i++) { pair
tmp; int l=n.first+dir[i][0]; int r=n.second+dir[i][1]; tmp.first=l; tmp.second=r; if(!mp[l][r]&&!vis[l][r]&&l>=0&&l<=4&&r>=0&&r<=4) { q.push(tmp); vis[l][r]=1; path[l][r].first=n.first; path[l][r].second=n.second; if(l==4&&r==4) return ; } } }}int main(){ for(int i=0;i<5;i++) for(int j=0;j<5;j++) scanf("%d",&mp[i][j]); memset(vis,0,sizeof(vis)); bfs(); putout(4,4); return 0;}

总结:

1.只输出路径:(1)开pair<>数组,存储前驱坐标 (2)struct内添加前驱

2.只输出步数:(1)struct添加step (2)mp数组存每一点步数

3.输出路径和步数:(1)pair<>存路径+mp[][]存步数 (2)struct存 当前坐标、前驱坐标、步数

 

先写个小总结。

NO2 NO3 未完待续 有空回来补

 

咕了

 

我又回来了!!!!!

NO2.结构体存:

#include 
using namespace std;typedef long long ll;const int N=4.5e6+20;bool vis[10][10];int a[10][10];int tot;int dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1}};struct node{ int x,y,id,pre;}s[50];void bfs(node n){ queue
q; q.push(n); vis[n.x][n.y]=1; while(q.size()) { node tmp=q.front(); q.pop(); if(tmp.x==4&&tmp.y==4) return; for(int i=0;i<4;i++) { int fx=tmp.x+dir[i][0]; int fy=tmp.y+dir[i][1]; if(fx>=0&&fx<5&&fy>=0&&fy<5&&!a[fx][fy]&&!vis[fx][fy]) { vis[fx][fy]=1; s[++tot].x=fx; s[tot].y=fy; s[tot].id=tot; s[tot].pre=tmp.id; q.push(s[tot]); if(fx==4&&fy==4) return ; } } }}void push(int id){ if(id==0) return ; push(s[id].pre); cout<<'('<
<<", "<
<<')'<<'\n';}int main(){ memset(a,0,sizeof(a)); for(int i=0;i<5;i++) for(int j=0;j<5;j++) scanf("%d",&a[i][j]); s[1].x=0; s[1].y=0; s[1].id=1; s[1].pre=0; s[1].id=1; tot=1; bfs(s[1]); push(s[tot].id); return 0;}

 

转载地址:http://nwio.baihongyu.com/

你可能感兴趣的文章
mabatis 中出现&lt; 以及&gt; 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>