POJ 3984 迷宫问题(bfs搜索,递归打印)
来源:互联网 发布:linux下配置环境变量 编辑:程序博客网 时间:2024/04/29 10:03
给定起点终点的寻找最短路径并打印。这是我自己写的模板。有意自取。
bfs每扩充一层,就把上层位置记录在node的pre中,最后递归打印。
用队列实现。
注意递归的调用顺序,到出口(最简计算)再返回。
代码:
#include<iostream>#include<cstring>#include<queue>using namespace std;int map[10][10];int dir[4][2]= {1,0,-1,0,0,1,0,-1};struct node{ int x,y,pre;//记录上层} q[105];bool judge(int x,int y){ if(x>=0&&x<=4&&!map[x][y]&&y>=0&y<=4)return 1; return 0;}void print(int x){ if(q[x].pre!=-1) { print(q[x].pre);//注意打印顺序 cout<<"("<<q[x].x<<", "<<q[x].y<<")"<<endl; }}void bfs(){ int front = 0,rear = 1; q[front].x=0; q[front].y=0; q[front].pre=-1; while(front<rear) { for(int i=0; i<4; i++) { int nx = q[front].x+dir[i][0]; int ny = q[front].y+dir[i][1]; if(judge(nx,ny)) { map[nx][ny]=1; q[rear].x=nx; q[rear].y=ny; q[rear++].pre=front; } if(nx==4&&ny==4)//找到最短回归 { cout<<"(0, 0)"<<endl; print(front); cout<<"(4, 4)"<<endl; return; } } front++; }}int main(){ for(int i=0; i<5; i++) for(int j=0; j<5; j++) cin>>map[i][j]; bfs();}
0 0
- POJ 3984 迷宫问题(bfs搜索,递归打印)
- poj 3984 迷宫问题 (BFS+递归)
- POJ 3984 迷宫问题(bfs+打印路径)
- poj 3984迷宫问题(bfs加打印路径)
- POJ-3984-迷宫问题(BFS打印路径)
- POJ 3984:迷宫问题 bfs+递归输出路径
- poj 3984 迷宫问题 BFS 图的广度优先搜索
- POJ 3984 迷宫问题 (BFS,水题)
- poj 3984 -- 迷宫问题 (bfs)
- POJ 3984 迷宫问题 (BFS)
- poj 3984 迷宫问题 (bfs入门)
- poj 3984 迷宫问题(bfs)
- POJ 3984 - 迷宫问题(BFS)
- POJ 题目3984 迷宫问题(BFS)
- Poj 3984 迷宫问题(BFS)
- POJ 3984 迷宫问题(BFS)
- POJ 3984 迷宫问题 (bfs 水)
- POJ 3984 迷宫问题(bfs)
- 详解HTTP中GET和POST的区别
- C语言编程实现使用AES对文件进行加密
- Socket网络通信
- 关于new 与 malloc的一个问题
- Java IO - CharArrayReader&CharArrayWriter
- POJ 3984 迷宫问题(bfs搜索,递归打印)
- CodeVS 1039 数的划分
- 格子刷油漆(DP)
- Tomcat启动:A fatal error has been detected by the Java Runtime Environment(JVM Crash分析及相关资料)
- PAT乙级—1047. 编程团体赛(20)-native
- 配置java 和tomacat环境过程
- C++实验6--数组合并
- 设计模式的学习——工厂模式
- Java 之类的加载顺序