数据结构实习 Problem H 迷宫的最短路径
数据结构实习 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 迷宫的最短路径的更多相关文章
- 数据结构实习 - Problem N 树的括号表示法
writer:pprp date:20171103 题目描述 先将根结点放入一对圆括号中,然后把它的子树按由左而右的顺序放入括号中,而对子树也采用同样方法处理:同层子树与它的根结点用圆括号括起来,同层 ...
- 数据结构实习 - problem M 判断平衡二叉树
writer:pprp date: 20171103 题目描述 给定一棵二叉树的中序和层序输出,判断是否为平衡二叉树的.如果是,输出YES如果不是输出NO. 输入 树结点个数 中序遍历序列 层序遍历序 ...
- 数据结构实习 problem O Huffman Tree
Huffman Tree 题目描述 对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码. 输入 大写字母个数 n 第一个字母 第二个字母 第三个字母 .. ...
- 数据结构实习 problem L 由二叉树的中序层序重建二叉树
由二叉树的中序层序重建二叉树 writer:pprp 用层序中序来重建二叉树 代码点这里 其实本质上与前序中序建立二叉树没有什么太大区别 大概思路: 递归解法,对当前层进行处理,通过层序遍历可以得到当 ...
- 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出
用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...
- Problem H
Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...
- 编程算法 - 迷宫的最短路径 代码(C++)
迷宫的最短路径 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定一个大小为N*M的迷宫. 迷宫由通道和墙壁组成, 每一步能够向邻接的上下 ...
- 实验12:Problem H: 整型数组运算符重载
Home Web Board ProblemSet Standing Status Statistics Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...
- 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. ...
随机推荐
- 全球数字货币交易所TOP20安全性评级报告
链塔智库2018-05-03 10:28 分析师:常昊.王婧雯 来源: 链塔智库 全球加密数字货币市值超2.5万亿元,单日交易额超2000亿元,全球超过3000万人已投入加密数字货币领域. ...
- Ansible安装过程中常遇到的错误(FAQ)
1.安装完成后允许命令报错 Traceback (most recent call last): File , in <module> (runner, results) = cli.ru ...
- Hdu 2457 DNA repair (ac自己主动机+dp)
题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...
- Oracle中查看建立索引和使用索引的注意点
一.查看和建立索引 select * from user_indexes where table_name = 'student' create index i_student_num on stud ...
- wpa安装方法
1.openssl 2.lib 1.1.2 3.wpa lua 编译错误 http://www.blogjava.net/xiaomage234/archive/2013/09/13/404037.h ...
- struct初始化
C语言中struct初始化 • 普通结构体的初始化 假设我们有如下的一段代码,其中已有Student结构体,要求实例化一个Student对象并将其初始化. #include <stdio.h&g ...
- sql 服务器链接远程 sql 服务器 脚本
exec sp_droplinkedsrvlogin 'test',null exec sp_dropserver 'test' exec sp_addlinkedserver@server='Tes ...
- Delphi APP 開發入門(二)Android/iOS設定,Hello World
Delphi APP 開發入門(二)Android/iOS設定,Hello World 分享: Share on facebookShare on twitterShare on google_plu ...
- 微信小程序组件slider
表单组件slider:官方文档 Demo Code: var pageData = {} for (var i = 1; i < 5; i++) { (function (index) { pa ...
- 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) B - Bribing Eve
地址:http://codeforces.com/gym/101174/attachments 题目:pdf,略 思路: 把每个人的(x1,x2)抽象成点(xi,yi). 当1号比i号排名高时有==& ...