华为OJ 迷宫问题
来源:互联网 发布:淘宝评价内衣图片 编辑:程序博客网 时间:2024/05/16 04:49
题目描述
定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。
Input
一个N × M的二维数组,表示一个迷宫。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
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)
输入描述:
输入两个整数,分别表示二位数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
输出描述:
左上角到右下角的最短路径,格式如样例所示。
输入例子:
5 50 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0
输出例子:
(0,0)(1,0)(2,0)(2,1)(2,2)(2,3)(2,4)(3,4)(4,4)
#include<iostream>#include<deque>using namespace std;struct pos{int x;int y;};int main(){int n, m;while (cin>>n>>m){int **a = new int*[n];for (int i = 0; i < n; i++)a[i] = new int[m];for (int i = 0; i < n;i++)for (int j = 0; j < m; j++)cin >> a[i][j];deque<pos> s;pos offset[4];//位置偏移offset[0].x = 0; offset[0].y = 1;//向右offset[1].x = 1; offset[1].y = 0;//向下offset[2].x = 0; offset[2].y = -1;//向左offset[3].x = -1; offset[3].y = 0;//向上pos here;here.x = 0;here.y = 0;s.push_back(here);s.push_back(here);int k ;bool flag = false;//是否搜索完毕的标志符while (!flag){ //如果没有找到,则返回上一个位置here = s.back();s.pop_back();for (k = 0; k < 4;k++){ //4个方向的搜索int r = here.x + offset[k].x;int c = here.y + offset[k].y;if (r == n - 1 && c == m - 1){ //找到出口则 将出口放入队列 并设置标识符及跳出循环flag = true;pos fin;fin.x = n - 1;fin.y = m - 1;s.push_back(fin);break;}if (r>=0&&r<=n-1&&c>=0&&c<=m-1&&a[r][c]==0){//如果当前位置为0且在搜索范围,则移动到当前位置,再接着搜索4个方向pos next;next.x = r; next.y = c;
a[r][c]=1;//设置为1,防止重复访问s.push_back(next);here = next;k = -1;//记得将K设置为-1}}}while (!s.empty())//输出所走路径{cout << '('<<s.front().x << ',' << s.front().y<<')'<< endl;s.pop_front();}}return 0;}二、广度优先遍历,方法类似只不过偏移量设置为2个 向右和向下其他一致
#include<iostream> #include<deque> using namespace std; struct pos { int x; int y; }; int main() { int n, m; while (cin>>n>>m) { int **a = new int*[n]; for (int i = 0; i < n; i++) a[i] = new int[m]; for (int i = 0; i < n;i++) for (int j = 0; j < m; j++) cin >> a[i][j]; deque<pos> s; pos offset[2]; offset[0].x = 0; offset[0].y = 1; //向右 offset[1].x = 1; offset[1].y = 0; //向下 pos here; here.x = 0; here.y = 0; s.push_back(here); s.push_back(here); int k ; bool flag = false; while (!flag) { here = s.back(); s.pop_back(); for (k = 0; k < 2;k++) { int r = here.x + offset[k].x; int c = here.y + offset[k].y; if (r == n - 1 && c == m - 1) { pos fin; fin.x = n - 1; fin.y = m - 1; s.push_back(fin); flag = true; break; } if (r>=0&&r<=n-1&&c>=0&&c<=m-1&&a[r][c]==0) { pos next; next.x = r; next.y = c; a[r][c]=1; s.push_back(next); here = next; k = -1; } } } while (!s.empty()) { cout << '('<<s.front().x << ',' << s.front().y<<')'<< endl; s.pop_front(); } } return 0; }
0 0
- 华为oj迷宫问题
- 华为oj--迷宫问题
- 【华为OJ】迷宫问题
- 华为OJ 迷宫问题
- 华为OJ--------迷宫问题
- 华为OJ迷宫问题
- 华为OJ(迷宫问题)
- 【华为OJ】【077-迷宫问题】
- 华为oj中级 迷宫问题
- 华为OJ——迷宫问题
- 华为OJ——迷宫问题
- 华为OJ 走迷宫 Java BFS
- 华为OJ 走迷宫 DFS Java
- Java算法---华为oj迷宫问题求解(深度优先搜索)
- Java算法---华为oj迷宫问题求解(广度优先搜索)
- 华为OJ 约瑟夫问题
- 华为OJ多线程问题
- 华为oj 百钱买百鸡问题
- Calendar类
- scala 判断字符串是否为数字
- CentOS7 从零搭建Spark 2.0 单机环境
- 让vim支持系统剪贴板
- python 学习 100_1
- 华为OJ 迷宫问题
- 头文件<>和""的区别
- flask-bootstrap/base.html研究以及使用
- Vert.x 核心模块(十六) 流 线程安全 SPI OSGi
- windos显著提高效率的黑科技软件
- Vert.x核心模块(十七) 命令行
- 利用阿里大于(大鱼)平台进行发送手机验证码
- 文章标题
- Java学习之异常处理