poj3984 迷宫问题
2015-08-12 10:46
218 查看
迷宫问题
Description
定义一个二维数组:
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
Sample Output
[code]
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11428 | Accepted: 6821 |
定义一个二维数组:
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)
本题主要是记录搜索时的路径,特地画了个简图,大家看看:
附代码:
[code]
#include<stdio.h> #include<queue> #include<algorithm> #include<iostream> using namespace std; int dx[4]={1,0,0,-1};//四个方向 int dy[4]={0,1,-1,0}; int f=0,r=1; int map[5][5]; struct line { int x,y,t; }road[1000],temp; bool judge(line a)//判断是否出界或是走过 { return a.x >=0&&a.x <5&&a.y >=0&&a.y <5&&!map[a.x ][a.y ]; } void PRINTF(int i)//递归输出路径 { if(road[i].t!=-1) { PRINTF(road[i].t ); printf("(%d, %d)\n",road[i].x,road[i].y ); } } void bfs() { road[f].x =0; road[f].y =0; road[f].t =-1; while(f<r)//控制并记录路径 { for(int i=0;i<4;i++) { temp.x =road[f].x +dx[i]; temp.y =road[f].y +dy[i]; if(!judge(temp)) continue; else { map[temp.x ][temp.y ]=1; road[r].x =temp.x ; road[r].y=temp.y; road[r].t= f; r++; } if(temp.x ==4&&temp.y ==4) PRINTF(f); }f++; } } int main() { for(int i=0;i<5;i++) for(int j=0;j<5;j++) scanf("%d",&map[i][j]); printf("(0, 0)\n"); bfs(); printf("(4, 4)\n"); return 0; }
</pre>
相关文章推荐
- [java] 匿名内部类
- 邮箱password复位图
- C语言中的malloc使用详解
- 共享内存
- Java基础总结之各个模块重要知识点
- json数据解析
- hdoj畅通工程
- iOS推送小结
- trie hdu1251 字典树
- 不敢死队问题
- 【Java设计模式】· 策略模式(Strategy Pattern)
- 【HDOJ 5371】 Hotaru's problem
- 了解你的家公家IP
- 自己封装的Metro风格的后台UI框架,暂命名MUI(持续更新)
- phpstorm 9 keygen
- 最近准备研读thinkphp源码,ctag派上用场了
- iOS UITextView内容添加后,自动滚动到最后一行
- C#线程池ThreadPool
- Mybatis_review之配置文件中的properties节点内容说明
- debian 常用命令说明