POJ3984(迷宫问题)
2016-07-13 21:17
363 查看
迷宫问题
Description
定义一个二维数组:
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15154 | Accepted: 9030 |
定义一个二维数组:
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 <string.h> using namespace std; const int MAXN=1000005; struct Node{ int y,x,pre; Node(){} Node(int cy,int cx,int cpre) { y=cy; x=cx; pre=cpre; } }que[MAXN]; int front,rear; int mz[5][5]; int vis[5][5]; int dy[4]={0,1,0,-1}; int dx[4]={1,0,-1,0}; void print(int id) { Node now=que[id]; if(now.pre==-1) { cout<<"("<<now.y<<", "<<now.x<<")"<<endl; return ; } print(now.pre); cout<<"("<<now.y<<", "<<now.x<<")"<<endl; } void bfs() { memset(vis,0,sizeof(vis)); front=rear=0; que[rear++]=Node(0,0,-1); vis[0][0]=1; while(front!=rear) { Node now=que[front++]; if(now.y==4&&now.x==4) { print(front-1); return ; } for(int i=0;i<4;i++) { int ny=now.y+dy[i]; int nx=now.x+dx[i]; if(0<=ny&&ny<5&&0<=nx&&nx<5&&!vis[ny][nx]&&mz[ny][nx]==0) { que[rear++]=Node(ny,nx,front-1); vis[ny][nx]=1; } } } } int main() { for(int i=0;i<5;i++) for(int j=0;j<5;j++) cin>>mz[i][j]; bfs(); return 0; }
相关文章推荐
- Python的图形化界面
- 最大流最小割问题的流量计算问题
- Redis数据库(set类型和zset类型)
- 关于本地连接远程github仓库的若干问题
- android webview快速获取页面上的图片
- Git(二)
- 获取网页中的视频下载地址(利用proxy_server)
- js事件处理(DOM0,DOM2,IE)
- netty学习(三)——开始传送对象喽!!
- hdu 5142(数学-进制转换)
- RT-Thread创建静态、动态线程
- 链表的头结点和尾节点的用处
- SearchView简单示例
- Python+Selenium+PIL+Tesseract真正自动识别验证码进行一键登录
- 最优配对问题
- 欢迎使用CSDN-markdown编辑器
- Day three
- git日志输出格式及两个版本之间差异列表
- Redis详解:lists数据类型及操作
- 【leetcode】165. Compare Version Numbers