数据结构实习 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. Spark源码分析 – Dependency

    Dependency 依赖, 用于表示RDD之间的因果关系, 一个dependency表示一个parent rdd, 所以在RDD中使用Seq[Dependency[_]]来表示所有的依赖关系 Dep ...

  2. django 前端传文件到后台项目目录

    Html端: <form action="/student/upload" method="POST" enctype="multipart/f ...

  3. Sublime Text 中文

    1.打开Sublime Text 2.Ctrl+Shift+P,输入Package Control: Install Package回车 3.输入LocalizedMenu,回车 4.点击菜单help ...

  4. python类可以任意添加属性

    python类可以任意添加属性 class A(object): def __init__(self): self.name = "zhangsan" self.age = 18 ...

  5. centos 配置yum源

    1.yum配置 yum的配置文件在  /etc/yum.conf [root@mini ~]# cat /etc/yum.conf [main] cachedir=/var/cache/yum/$ba ...

  6. OCR技术浅探: 光学识别(3)

    经过前面的文字定位和文本切割,我们已经能够找出图像中单个文字的区域,接下来可以建立相应的模型对单字进行识别. 模型选择 在模型方面,我们选择了深度学习中的卷积神经网络模型,通过多层卷积神经网络,构建了 ...

  7. MySQL 最基本的SQL语法/语句

    DDL—数据定义语言(Create,Alter,Drop,DECLARE) DML—数据操纵语言(Select,Delete,Update,Insert) DCL—数据控制语言(GRANT,REVOK ...

  8. 史上最有魄力公司!20亿主要用于团队建设,要在上海做出一家BAT之外的互联网公司

    在去年的创业大军里,有一家公司显得很特别——微鲸科技,背靠华人文化,联合阿里巴巴.腾讯和央广,天使轮就高达20亿,是被誉为互联网电视领域的豪华创业团队. 在上市不到半年的时间里,旗下发布的55吋和43 ...

  9. iOS 定位方式 iOSNsPredicateString 详解

    原文地址https://segmentfault.com/a/1190000010205649 前言 由于使用id.className.AccessibilityId定位方式较为简单,多数情况下,在同 ...

  10. CSS 换行知多少: word-wrap && word-break && white-space && word-spacing

    word-wrap : 首先提一下,word-wrap 这个 CSS 属性在CSS3中已经被更名为 overflow-wrap,这样语义化也是为了避免与 word-break 混淆: Referenc ...