POJ 3984 迷宫问题
2015-08-17 17:17
211 查看
迷宫问题
Description
定义一个二维数组:
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
Sample Output
一个BFS的水题,适合初学数据结构的做一做。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11501 | Accepted: 6870 |
定义一个二维数组:
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)
一个BFS的水题,适合初学数据结构的做一做。
/************************************************************************* > File Name: 迷宫问题.cpp > Author: Zhanghaoran0 > Mail: chiluamnxi@gmail.com > Created Time: 2015年08月17日 星期一 16时23分14秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int map[6][6]; int sx, sy, ex, ey; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0 ,-1, 1}; struct node{ int x; int y; int lastn; }q[1000001]; bool flag[6][6]; void print(int x){ if(q[x].lastn != -1){ print(q[x].lastn); } printf("(%d, %d)\n", q[x].x, q[x].y); } void bfs(){ memset(flag, 0, sizeof(flag)); int head = 0; int tail = 1; q[head].x = sx; q[head].y = sy; q[head].lastn = -1; while(head < tail){ for(int i = 0; i < 4; i ++){ int tempx = q[head].x + dx[i]; int tempy = q[head].y + dy[i]; if(flag[tempx][tempy] || tempx < 0 || tempx >= 5 || tempy < 0 || tempy >= 5 || map[tempx][tempy] == 1) continue; q[tail].x = tempx; q[tail].y = tempy; q[tail].lastn = head; flag[tempx][tempy] = true; if(tempx == ex && tempy == ey){ print(tail); } tail ++; } head ++; } } int main(void){ for(int i = 0; i < 5; i ++){ for(int j = 0; j < 5; j ++){ cin >> map[i][j]; } } sx = 0; sy = 0; ex = 4; ey = 4; bfs(); return 0; }
相关文章推荐
- Maven 下载net.sf.json-lib jar包
- POJ 3984 迷宫问题
- 在构造方法中存在异常的惯用处理法
- bzoj2534: Uva10829L-gap字符串
- 20返回指针的函数与指向函数的指针
- Server 层及整个MySQL体系图
- linux 环境问题记录
- IntelliJ IDEA 快捷键-Java开发者你们都会吗?
- OBjective-C:atomic和nonatomic的区别
- 解决Cannot change version of project facet Dynamic web module to 3.0
- 19指针和字符串
- 集合栈计算机
- [leetcode][deque] Sliding Window Maximum
- SQL Server游标的使用【转】
- 通过USB连接线ssh到iOS中
- ubuntu 12.04下搭建web服务器(MySQL+PHP+Apache) 教程
- 如何通过ContentHub把内容从一个地方传到另外一个地方
- Javascript、Jquery获取浏览器和屏幕各种高度宽度
- Robot Framework 教程 (1) - 环境配置及简单网站兼容性测试
- 编写高质量代码改善C#程序的157个建议——建议60:重新引发异常时使用Inner Exception