poj 3984 迷宫问题
2011-07-05 10:25
369 查看
题意:中文的,不解释了
思路:唯一的难点是如何处理最短路径的输出,我的解决办法是将最短的路径储存在链表里,然后放到堆栈了保护起来+倒序一下
思路:唯一的难点是如何处理最短路径的输出,我的解决办法是将最短的路径储存在链表里,然后放到堆栈了保护起来+倒序一下
#include <iostream> #include <cstdio> #include <cstring> #include <stack> using namespace std; struct Path { int x, y; Path *forward; Path() {x=0; y=0; forward=NULL;} }path; stack<Path> st; int mov[][2]={{1, 0}, {-1, 0}, {0, -1}, {0, 1}}, cnt=1000000; bool vis[5][5]; int map[5][5]; void dfs(Path p, int step) { if(p.x==4 && p.y==4) { if(step<cnt) { cnt=step; path=p; while(!st.empty()) st.pop(); while(path.forward) {st.push(path); path=*path.forward;} } return; } for(int i=0; i<4; i++) { int xh=p.x+mov[i][0]; int yh=p.y+mov[i][1]; if(xh>=0 && xh<5 && yh>=0 && yh<5 && !vis[xh][yh] && !map[xh][yh]) { Path ph; ph.x=xh; ph.y=yh; ph.forward=&p; vis[xh][yh]=1; dfs(ph, step+1); vis[xh][yh]=0; } } } int main() { for(int i=0; i<5; i++) for(int j=0; j<5; j++) scanf("%d", &map[i][j]); memset(vis, 0, sizeof(vis)); dfs(path, 0); printf("(0, 0)\n"); while(!st.empty()) { path=st.top(); st.pop(); printf("(%d, %d)\n", path.x, path.y); } return 0; }
相关文章推荐
- poj 3984- 迷宫问题
- POJ 3984 迷宫问题
- 迷宫问题-POJ - 3984
- POJ 3984 迷宫问题
- POJ 3984 迷宫问题
- POJ 3984 迷宫问题
- G-POJ-3984 迷宫问题
- http://poj.org/problem?id=3984 迷宫问题
- POJ 3984 迷宫问题
- POJ 3984 迷宫问题
- POJ - 3984 迷宫问题(自己待ac)
- POJ-3984 (迷宫问题)
- POJ 3984 迷宫问题 (BFS,水题)
- POJ 3984 迷宫问题 bfs
- POJ 3984:迷宫问题【BFS】
- POJ 3984 迷宫问题 (Dijkstra)
- poj 3984迷宫问题
- POJ-3984-迷宫问题(BFS)
- K_迷宫问题(POJ_3984)
- POJ 3984 迷宫问题(bfs)