数据结构实习 Problem H 迷宫的最短路径

题目描述

设计一个算法找一条从迷宫入口到出口的最短路径。

输入

迷宫的行和列m n

迷宫的布局

输出

最短路径

样例输入

6 8

0 1 1 1 0 1 1 1

1 0 1 0 1 0 1 0

0 1 0 0 1 1 1 1

0 1 1 1 0 0 1 1

1 0 0 1 1 0 0 0

0 1 1 0 0 1 1 0

样例输出

(6,8)

(5,7)

(4,6)

(4,5)

(3,4)

(3,3)

(2,2)

(1,1)

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector> using namespace std;
const int maxn = 1000; int dx[] = {1,1,0,-1,-1,-1,0,1};
int dy[] = {0,-1,-1,-1,0,1,1,1};
int n, m;
int ** maze;
int ** vis; struct node
{
int x, y;
node(int a, int b):x(a),y(b){}
node():x(0),y(0){}
}; node pre[maxn][maxn];
queue<node> qu;
vector<node> a; void BFS()
{
node cur;
while(!qu.empty())qu.pop();
int c,r;
//记录路径
cur.x = 1,cur.y = 1;
qu.push(cur);
vis[1][1] = 1;
while(!qu.empty())
{
cur = qu.front();
qu.pop();
//判断是否已经达到终点
if(cur.x == n && cur.y == m)
{
a.push_back(node(n,m));
while(cur.x != 1||cur.y != 1)
{
r = cur.x;
c = cur.y;
a.push_back(node(pre[r][c].x,pre[r][c].y));
cur.x = pre[r][c].x,cur.y = pre[r][c].y;
}
for(int i = 0 ; i < a.size()-1; i++)
printf("(%d,%d)\n", a[i].x, a[i].y);
return ;
}
for(int i = 0 ; i < 8; i++)
{
//方向调整
r = cur.x + dx[i];
c = cur.y + dy[i];
if(r >= 1 && r <= n && c >= 1 && c <= m
&& vis[r][c] == 0 && maze[r][c] == 0)
{
vis[r][c] = vis[cur.x][cur.y]+1;
pre[r][c] = node(cur.x,cur.y);
qu.push(node(r,c));
}
}
}
} void print()
{
for(int i = 0; i < n + 2; i++)
{
for(int j = 0 ; j < m + 2; j++)
{
cout << maze[i][j] << " ";
}
cout << endl;
}
cout << endl;
for(int i = 0 ; i < n+2; i++)
{
for(int j = 0 ; j < m+2; j++)
{
cout << vis[i][j] << " ";
}
cout << endl;
}
return;
} int main()
{
freopen("in.txt","r",stdin);
cin >> n >> m;
maze = new int* [n+2];
vis = new int* [n+2];
for(int i = 0 ; i < n + 2; i++)
{
maze[i] = new int[m+2];
vis[i] = new int[m+2];
}
for(int i = 0 ; i < n+2; i++)
for(int j = 0 ; j < m+2; j++)
{
maze[i][j] = 1;
vis[i][j] = 1;
}
for(int i = 1; i <= n ; i++)
for(int j = 1; j <= m ; j++)
{
cin >> maze[i][j];
vis[i][j] = 0;
}
BFS();
cout << "(1,1)" << endl;
return 0;
}

数据结构实习 Problem H 迷宫的最短路径的更多相关文章

  1. 数据结构实习 - Problem N 树的括号表示法

    writer:pprp date:20171103 题目描述 先将根结点放入一对圆括号中,然后把它的子树按由左而右的顺序放入括号中,而对子树也采用同样方法处理:同层子树与它的根结点用圆括号括起来,同层 ...

  2. 数据结构实习 - problem M 判断平衡二叉树

    writer:pprp date: 20171103 题目描述 给定一棵二叉树的中序和层序输出,判断是否为平衡二叉树的.如果是,输出YES如果不是输出NO. 输入 树结点个数 中序遍历序列 层序遍历序 ...

  3. 数据结构实习 problem O Huffman Tree

    Huffman Tree 题目描述 对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码. 输入 大写字母个数 n 第一个字母 第二个字母 第三个字母 .. ...

  4. 数据结构实习 problem L 由二叉树的中序层序重建二叉树

    由二叉树的中序层序重建二叉树 writer:pprp 用层序中序来重建二叉树 代码点这里 其实本质上与前序中序建立二叉树没有什么太大区别 大概思路: 递归解法,对当前层进行处理,通过层序遍历可以得到当 ...

  5. 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出

    用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...

  6. Problem H

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...

  7. 编程算法 - 迷宫的最短路径 代码(C++)

    迷宫的最短路径 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定一个大小为N*M的迷宫. 迷宫由通道和墙壁组成, 每一步能够向邻接的上下 ...

  8. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

  9. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

随机推荐

  1. 转:Java并发编程与技术内幕:线程池深入理解

    版权声明:本文为博主林炳文Evankaka原创文章,转载请注明出处http://blog.csdn.net/evankaka 目录(?)[+] ); } catch (InterruptedExcep ...

  2. HDFS集群安装

    DFS集群安装: 1.准备工作 (1)虚拟机(电脑8G 磁盘500GB) (2)3台linux系统(1台namenode 2台datanode) 2.安装HDFS(软件) (1)关闭防火墙 firew ...

  3. css冲突2 要关闭的css在项目代码以外,但是是通过<link>标签引入的css(例如bootstrap):解决方法,在APP.css中使用全局样式

    css冲突,导致html字体过小. 通过浏览器检查发现,导致字体过小的css来自bootstrap. 现要关闭bootstrap的css: 直接在APP.css中添加: html{ font-size ...

  4. 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活--hdu2191(多重背包模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 标准的多重背包 题目 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是 ...

  5. JSP学习(第二课)

    把GET方式改为POST在地址栏上就不会显示. 发现乱码了,设置编码格式(这个必须和reg.jsp中page的charset一致):  但是注意了!我们传中文名,就会乱码: 通过get方式提交的请求无 ...

  6. wireshark抓TCP包

    tcpdump下载 如果要抓TCP数据包,我们可以使用TCPdump工具,类似于windows/linux下使用的这个工具一样.具体方法是 下载tcpdump, 还有个下载地址 详细使用请参考里面的文 ...

  7. Mysql binlog 安全删除(转载)

    简介: 如果你的 Mysql 搭建了主从同步 , 或者数据库开启了 log-bin 日志 , 那么随着时间的推移 , 你的数据库 data 目录下会产生大量的日志文件 shell > ll /u ...

  8. 我与前端之间不得不说的三天两夜之jQuery

    前端基础之jquery 一 jQuery是什么? [1] jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2] jQuery是 ...

  9. SQL Server 使用触发器(trigger)发送电子邮件

    sql 使用系统存储过程 sp_send_dbmail 发送电子邮件语法: sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @r ...

  10. 中线,基线,垂直居中vertical-align:middle的一些理解

    基线:小写字母xxxxx的下边缘线就是我们的css基线:一般的行内元素都是vertical-align: baseline;默认设置: x-height:就是指小写字母xxxx的高度,下边缘线到上边缘 ...