poj 3984 迷宫问题 BFS+路径记录
2017-06-16 23:20
399 查看
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<stack> using namespace std; struct node { int v; int fx,fy; }; node map[10][10]; bool vis[10][10]; int dirx[4]={ 1,-1, 0, 0}; int diry[4]={ 0, 0, 1,-1}; int main() { for(int i=1;i<=5;i++) { for(int j=1;j<=5;j++) { scanf("%d",&map[i][j].v); } } queue<node> q; map[1][1].fx=1; map[1][1].fy=1; node start; start.fx=1; start.fy=1; q.push(start); vis[1][1]=1; while(!q.empty()) { node tmp=q.front(); // cout << tmp.fx <<','<<tmp.fy<<endl; q.pop(); for(int i=0;i<4;i++) { node next=tmp; next.fx+=dirx[i]; next.fy+=diry[i]; if(next.fx<1 || next.fx>5) continue; if(next.fy<1 || next.fy>5) continue; if(map[next.fx][next.fy].v==1) continue; if(vis[next.fx][next.fy]) continue; vis[next.fx][next.fy]=1; map[next.fx][next.fy].fx=tmp.fx; map[next.fx][next.fy].fy=tmp.fy; if(next.fx==5 && next.fy==5) break; q.push(next); } } int curx=5,cury=5; stack<node> ans; while(!(curx==1 && cury==1)) { node Ans; Ans.fx=curx; Ans.fy=cury; ans.push(Ans); int tmpx=map[curx][cury].fx; int tmpy=map[curx][cury].fy; curx=tmpx; cury=tmpy; } printf("(0, 0)\n"); while(!ans.empty()) { node Ans=ans.top(); ans.pop(); printf("(%d, %d)\n",Ans.fx-1,Ans.fy-1); } // for(int i=1;i<=5;i++) // { // for(int j=1;j<=5;j++) // { // printf("(%d,%d) ",map[i][j].fx,map[i][j].fy); // } // cout << endl; // } }这道题 可以不用struct记录路径 可以直接用数字记录信息 因为数字很小 所有完全可以用一个四位数记录,前两位代表行,后两位代表列
相关文章推荐
- 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 迷宫问题 (路径记录)
- Poj 3984 迷宫问题(BFS+DFS打印路径)
- POJ 3984 迷宫问题 BFS 记录路径
- POJ-3984 迷宫问题 (bfs+路径输出)
- POJ 3984 迷宫问题(BFS+路径输出)
- POJ 3984迷宫问题(记录路径)
- POJ - 3984 迷宫问题 —— BFS记录路径
- poj 3984 迷宫问题 (BFS+记录路径)
- poj_3984_迷宫问题_(bfs+记录路径)
- POJ 3984 迷宫问题(记录路径的搜索)
- poj 3984 迷宫问题 【BFS + 优先队列 + stack路径记录】