POJ
来源:互联网 发布:python map 源码 编辑:程序博客网 时间:2024/06/09 17:54
迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 24702 Accepted: 14422
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)
最短路一般用宽搜
当时还比较年轻 用了一个二维的结构体数组储存每个点的上一步是哪里 其实用一个一维的结构体数组记录每一步走到了那里就好
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define max 10using namespace std;struct z{int x,y,vis;}num[max*max],ans,step[max][max];int n=5,map[max][max],dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};void show(int x,int y){if(x==0&&y==0)scanf("(0, 0)\n");else{show(step[x][y].x,step[x][y].y);printf("(%d, %d)\n",step[x][y].x,step[x][y].y);}}void bfs(){queue<struct z>q;q.push(num[0]);num[0].vis=0;int k;while(q.size()){ans=q.front();if(ans.x==4&&ans.y==4){show(ans.x,ans.y);printf("(4, 4)\n");break;}q.pop();for(k=0;k<4;k++){int tx=ans.x+dir[k][0];int ty=ans.y+dir[k][1];if(tx>=0&&ty>=0&&tx<n&&ty<=n&&map[tx][ty]==0&&num[tx*5+ty].vis==1){q.push(num[tx*5+ty]);map[tx][ty]=1;step[tx][ty].x=ans.x;step[tx][ty].y=ans.y;}}}}int main(){int i,j;for(i=0;i<n;i++){for(j=0;j<n;j++){cin>>map[i][j];num[i*5+j].x=i;num[i*5+j].y=j;num[i*5+j].vis=1;}}bfs();}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 重启eclipse color theme失效的解决办法
- 十四、Shell函数参数
- ubuntu下pycham的安装和使用
- jenkins在windows下的安装
- ViewStub的实现深入解析
- POJ
- PHP正则验证中文
- JavaBean的生命周期
- SQL Exercises/The warehouse
- Python-图像加噪实现(Gaussian noise+salt and pepper noise)
- 数据库
- 配置Tomcat使用https协议
- 调用电话薄打电话
- Android Html.fromHtml(String)过时的替代方法