poj3984 迷宫问题
来源:互联网 发布:纱布收腹带淘宝网 编辑:程序博客网 时间:2024/06/05 16:19
迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21186 Accepted: 12413
Description
定义一个二维数组:
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
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 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0
Sample Output
(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)
//// Created by Admin on 2017/5/3.////http://blog.csdn.net/raphealguo/article/details/7523411#include <cstdio>#include <cstring>struct node{ int x,y; //坐标x,y int pre;};node que[120],next;int visit[5][5]; //顶点访问状态int maze[5][5];int isValid(node vn){ //如果有效则返回1,否则返回0 if(vn.x<0||vn.y<0||vn.x>4||vn.y>4||maze[vn.x][vn.y]==1) return 0; else return 1;}void prtresult(int i){ //递归回溯输出路径 if(que[i].pre!=-1){ prtresult(que[i].pre); printf("(%d, %d)\n",que[i].x,que[i].y); }}void BFS(int x,int y){ memset(visit,0, sizeof(visit)); int front=0,rear=1; que[front].x=x; que[front].y=y; que[front].pre=-1; visit[x][y]=1; int dir[][2]={ //移动方向 {0,1},{1,0}, {0,-1},{-1,0} }; while (front<rear){ for (int i = 0; i < 4; ++i) { next.x=que[front].x+dir[i][0]; next.y=que[front].y+dir[i][1]; if(next.x==4&&next.y==4){ prtresult(front); return; } if(isValid(next)&&!visit[next.x][next.y]){ que[rear].x=next.x; que[rear].y=next.y; que[rear++].pre=front; visit[next.x][next.y]=1; } } front++; }}int main(){ while (~scanf("%d%d%d%d%d",&maze[0][0],&maze[0][1],&maze[0][2],&maze[0][3],&maze[0][4])){ for (int i = 1; i < 5; ++i) for (int j = 0; j < 5; ++j) scanf("%d",&maze[i][j]); printf("(0, 0)\n"); BFS(0,0); printf("(4, 4)\n"); } return 0;}
0 0
- POJ3984 迷宫问题 BFS
- POJ3984 迷宫问题
- HUD--POJ3984 迷宫问题
- POJ3984 迷宫问题 BFS
- poj3984 迷宫问题
- poj3984(迷宫问题)
- poj3984 迷宫问题
- [POJ3984]迷宫问题
- poj3984 迷宫问题(BFS)
- POJ3984 迷宫问题
- POJ3984 迷宫问题【BFS】
- poj3984 迷宫问题
- Poj3984- dfs -迷宫问题
- poj3984迷宫问题(bfs)
- poj3984迷宫问题
- poj3984 迷宫问题
- poj3984迷宫问题
- POJ3984 迷宫问题【BFS】
- PCA算法的数学原理
- 初学Vue笔记
- html input设置为只允许输入数字和小数点
- stringTools 字符串工具类
- gtk-3.0学习笔记2-hellword
- poj3984 迷宫问题
- 好用的分页器(原生)
- Android开发新手之ActionBar那些事
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 关于S2-045漏洞,由struts2.5升级到struts2.5.10.1的步骤
- RHEL 7.3 yum使用光盘安装
- 生产环境-jvm内存溢出-jprofile问题排查
- thinkphp 在lnmp环境下的url重写配置
- poj1552——Doubles