POJ 3984 迷宫问题(BFS+路径记录)
2017-12-09 18:07
246 查看
题目链接
思路:因为是要求最小路径,所以联想到用bfs,但是问题在如何记录路径,联想到可以开一个三维数组来记录路径。
dp[x2][y2][0]=x1;dp[x2][y2][1]=y1;
代码如下:
个人觉得这样好理解但是可能效率不高,引用一下这位大佬的。模拟队列实现,资源利用率较高。
dalao
思路:因为是要求最小路径,所以联想到用bfs,但是问题在如何记录路径,联想到可以开一个三维数组来记录路径。
dp[x2][y2][0]=x1;dp[x2][y2][1]=y1;
代码如下:
#include<iostream> #include<cstring> #include<string> #include<queue> using namespace std; int map[5][5],mo[4][2]={{1,0},{-1,0},{0,1},{0,-1}},dp[5][5][2]; bool vst[5][5]; struct point{ int x,y; point(int xx,int yy){ x=xx; y=yy; } }; queue<point>q; void bfs(int x,int y){ vst[x][y]=true; q.push(point(x,y)); while(!q.empty()){ //cout<<1; int x1=q.front().x; int y1=q.front().y; q.pop(); for(int i=0;i<4;i++){ //cout<<1; int x2=x1+mo[i][0]; int y2=y1+mo[i][1]; if(!vst[x2][y2]&&x2>=0&&y2>=0&&x2<=4&&y2<=4&&map[x2][y2]!=1){ vst[x2][y2]=true; q.push(point(x2,y2)); dp[x2][y2][0]=x1; dp[x2][y2][1]=y1; //cout<<x1<<","<<y1<<endl; } } } } void print(int x1,int y1){ if(x1==0&&y1==0){return ;} print(dp[x1][y1][0],dp[x1][y1][1]); cout<<"("<<dp[x1][y1][0]<<", "<<dp[x1][y1][1]<<")"<<endl; } int main(){ for(int i=0;i<5;i++) for(int j=0;j<5;j++) cin>>map[i][j]; bfs(0,0); print(4,4); cout<<"(4, 4)\n"; }
个人觉得这样好理解但是可能效率不高,引用一下这位大佬的。模拟队列实现,资源利用率较高。
dalao
相关文章推荐
- 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 迷宫问题 广搜记录路径
- J - 迷宫问题 POJ - 3984 (记录走过的路径)
- POJ 3984 迷宫问题(BFS+路径输出)
- poj_3984_迷宫问题_(bfs+记录路径)
- POJ 3984迷宫问题(记录路径)
- POJ 3984 迷宫问题 BFS 记录路径
- poj 3984 迷宫问题 (BFS+记录路径)
- POJ 3984 迷宫问题 (路径记录)
- POJ 3984--迷宫问题【BFS && 记录路径】
- POJ-3984 迷宫问题 (bfs+路径输出)
- POJ 3984 迷宫问题(记录路径的搜索)
- poj 3984 迷宫问题(BFS+路径输出)