POJ - 3984 - 迷宫问题
来源:互联网 发布:淘宝网木质沙发 编辑:程序博客网 时间:2024/05/29 03:22
POJ 3984.迷宫问题
Description
定义一个二维数组:
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 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
一直很怕的迷宫问题,还是得多练啊
#include<iostream>#include<cstdio>#include<string.h>#include<queue>using namespace std;int map[5][5];int vis[5][5];int dir[4][2]={{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; //因为是从终点往起点搜,所以按逆时针方向走 struct NODE{ int x, y, no, next; //结点x, y为坐标,no为该坐标在node[50]数组的下标,next为输出路径时下一个结点的下标 }node[50];void bfs(){ int tot = 0; node[0].x = node[0].y = 4; node[0].no = node[0].next = 0; //因为要正像输出路径,所以从终点反向往起点搜 queue<NODE> q; //用于保存路径 q.push(node[0]); vis[4][4] = 1; //标记已经走过的 while(!q.empty()){ NODE now = q.front(); q.pop(); if (now.x == 0 && now.y == 0){ //到起点,则打印路径 int t = now.no; while(1){ //因为node数组中每个结点中都有 printf("(%d, %d)\n", node[t].x, node[t].y); if (!t) return; t = node[t].next; } } for(int i = 0; i < 4; i++){ int nx = now.x + dir[i][0]; //nx, ny下一节点坐标 int ny = now.y + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && !map[nx][ny] && !vis[nx][ny]){ vis[nx][ny] = 1; //一个节点入队时,标记已经访问,并把该节点信息存入node数组 node[++tot].x = nx; node[tot].y = ny; node[tot].no = tot; node[tot].next = now.no; q.push(node[tot]); } } }}int main(){ memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); memset(node, 0, sizeof(node)); while(scanf("%d %d %d %d %d", &map[0][0], &map[0][1], &map[0][2], &map[0][3], &map[0][4])!=EOF){ for (int i = 1; i < 5; i++) for (int j = 0; j < 5; j++) scanf("%d", &map[i][j]); bfs(); } return 0;}
0 0
- poj 3984 迷宫问题
- poj 3984 迷宫问题
- POJ-3984-迷宫问题
- Poj 3984 迷宫问题
- 迷宫问题 poj 3984
- poj 3984迷宫问题
- poj 3984 迷宫问题
- POJ-3984-迷宫问题
- poj 3984- 迷宫问题
- POJ 3984 迷宫问题
- poj 3984 迷宫问题
- poj 3984:迷宫问题
- POJ-3984-迷宫问题
- POJ 3984 迷宫问题
- poj 3984迷宫问题
- POJ 3984 迷宫问题
- POJ 3984 迷宫问题
- poj 3984 迷宫问题
- JNDI(2)
- GET与POST传输的区别
- uva 12125 March of the Penguins (最大流)
- Oracle存储过程单步调试方法
- 注册mysql为系统服务and命令行启动
- POJ - 3984 - 迷宫问题
- Qt 多线程之MoveToThread()
- linux随笔-3
- PHP学习总结(一)
- 面试中经常出现的算法1(整理)
- SQLite多线程使用总结
- grails+oracle 一次实际操作记录
- UIScreen获取两种类型画面尺寸
- Java实现文件拷贝的4种方法