最近力扣刷了一些广度优先,深度优先的题目,看了b站的奇乐编程学院的一个寻路算法视频,突然想到这个知识点在离散的课堂上也讲过,从(0,0)到(n,n)要走多少步,其中还包括一些特殊问题,比如不能通过对角线,有障碍物等。

卡特兰数

学过离散的孩子应该都知道这个神奇的东西,我理解的卡特兰数是一个满足很多现实情况的数列。比如一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列? 又比如依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?…比较经典的问题就是今天要讨论的从(0,0)到(n,n)一共有多少种方式或者是最短路径是什么。卡特兰数是有公式可以解决的,这里先给出求卡特兰数c++的应用。

void catalan() //求卡特兰数
{int i, j, len, carry, temp;a[1][0] = b[1] = 1;len = 1;for(i = 2; i <= 100; i++){for(j = 0; j < len; j++) //乘法a[i][j] = a[i-1][j]*(4*(i-1)+2);carry = 0;for(j = 0; j < len; j++) //处理相乘结果{temp = a[i][j] + carry;a[i][j] = temp % 10;carry = temp / 10;}while(carry) //进位处理{a[i][len++] = carry % 10;carry /= 10;}carry = 0;for(j = len-1; j >= 0; j--) //除法{temp = carry*10 + a[i][j];a[i][j] = temp/(i+1);carry = temp%(i+1);}while(!a[i][len-1]) //高位零处理len --;b[i] = len;}
}

广度优先

广度优先大家都很熟了,而且寻道这类问题广度优先几乎是万能解法,当然了既然是万能解法那往往效率就不是最高的。从起点开始把起点周围四个点进队,每次从队首拿一个出来并标记为已访问,直到队列为空则跳出循环。

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
using namespace std;const int Min = 0;
const int Max = 10;
int Map[Max + 1][Max + 1] = { 0 };
int mark[Max + 1][Max + 1] = { 0 };
class find_path {public:void dfs(pair<int,int> begin,pair<int,int>end) {//Exception//...queue<pair<int, int>>qu;qu.emplace(begin);while (!qu.empty()) {pair<int, int>temp = qu.front();qu.pop();if (temp.first-1 >= Min && !Map[temp.first-1][temp.second ]) {pair<int, int>up(temp.first-1, temp.second );if (up == end)break;qu.emplace(up);Map[temp.first-1][temp.second ] = 1;mark[temp.first-1][temp.second ] = 1;}if (temp.first+1 <=Max && !Map[temp.first+1][temp.second ]) {pair<int, int>down(temp.first+1, temp.second );if (down == end)break;qu.emplace(down);Map[temp.first+1][temp.second ] = 1;mark[temp.first+1][temp.second ] = 2;}if (temp.second-1 >= Min && !Map[temp.first ][temp.second-1]) {pair<int, int>left(temp.first , temp.second-1);if (left == end)break;qu.emplace(left);Map[temp.first][temp.second-1] = 1;mark[temp.first][temp.second-1] = 3;}if (temp.second + 1 >= Min && !Map[temp.first][temp.second + 1]) {pair<int, int>right(temp.first , temp.second+1);if (right == end)break;qu.emplace(right);Map[temp.first][temp.second + 1] = 1;mark[temp.first][temp.second + 1] = 4;}}}
};int main() {find_path test;pair<int, int>begin(1, 1);pair<int, int>end(6, 6);Map[begin.first][begin.second] = 1;mark[begin.first][begin.second] = 5;mark[end.first][end.second] = 6;test.dfs(begin,end);for (int i = 0; i < Max + 1; i++) {for (int j = 0; j < Max + 1; j++) {switch (mark[i][j]) {case 1:cout << "↑ "; break;case 2:cout << "↓ "; break;case 3:cout << "← "; break;case 4:cout << "→ "; break;case 5:cout << "起 "; break;case 6:cout << "终 "; break;}}cout << endl;}}

写的确实有点啰嗦了,四个if直接暴露了我编程新手的现实。这里没有增加障碍物,当然也并不麻烦,只需要提前把障碍物的点的map设为1即可。

A star 算法

这是在视频里面看到的,据视频所说这是最常见的算法,它比广度优先高级的地方在于增加了一个花费(感觉有点像贪心算法)把当前花费加上预估花费作为判断队列出队的条件,花费少的先出队。这可以帮助我们尽快找到最短路径。其实也就是把广度优先算法中的队列换成优先队列即可,代码以后再补充吧。

这里贴出视频网址https://www.bilibili.com/video/BV1bv411y79P。

从(0,0)到(n,n)——广度优先及其改进相关推荐

  1. ERROR: Failed to resolve: com.android.support:appcompat-v7:29.0.0

    错误内容如下 ERROR: Failed to resolve: com.android.support:appcompat-v7:29.0.0 Show in Project Structure d ...

  2. No cached version of com.android.tools.build:gradle:2.0.0 available for offline mode.

    异常场景 从AS2.0升级到2.1,重新编译工程后,抛出了如下异常 Error:A problem occurred configuring root project 'AndroidStudioPr ...

  3. Can't connect to MySQL server on '127.0.0.1' (10061) (code 2003)解决方法

    先验证一下MySQL的服务是否开启,到计算机->管理->服务和应用程序->服务 如果服务已开启,就检查一下C:\WINDOWS\system32\drivers\etc目录下的hos ...

  4. pycharm中报错:Error: failed to send plot to http://127.0.0.1:63342

    pycharm中报错:Error: failed to send plot to http://127.0.0.1:63342 import matplotlib.pyplot as plt impo ...

  5. mysql h 127.0.0.1_MySQL 连接时尽量使用 127.0.0.1 而不是 localhost

    原因 Whenever you specify "localhost" or "localhost:port" as server, the MySQL cli ...

  6. 【记录】我在团队合作中遇到过的胎神(扑街仔)级别前端小伙伴 之 莫名其妙配置0.0.0.0这种IP访问

    废话不说上代码 K同学和Z同学,你们知道吗?你这样加0.0.0.0 host带来的结果就是npm run dev项目的时候莫名其妙会在浏览器里面弹出一个0.0.0.0的访问 K同学和Z同学,你们脑子进 ...

  7. 在Win 8.1上安装配置FlashDevelop5.0.0

    FlashDevelop是基于.NET框架的开源软件,只能在Windows环境下面运行,相较于Flash CS和FlexBuilder, 它非常轻量级.FlashDevelop只是一个代码编辑器,而不 ...

  8. Varnish Cache 3.0.0安装

    https://www.varnish-cache.org/installation/redhat Installation on RedHat 先按需要的模块 在安装软件包之前首先看看主机上的 au ...

  9. CentOS7安装配置redis-3.0.0

    一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar ...

  10. nero linux iso,NeroLINUX下载_NeroLINUX官方下载_NeroLINUX4.0.0.0-华军软件园

    Nero Linux 3是Linux 操作系统上权威的刻录应用程序.采用业界领先的Nero Burning ROM引擎,Nero Linux 3成为Linux上最强大而多功能的刻录软件,而且也是唯一一 ...

最新文章

  1. Codechef REBXOR[dp+字典树]
  2. sql 只取一条记录_后端程序员必备:书写高质量SQL的30条建议
  3. 使用ASP.Net 3.5 的Ajax与Web服务开发实例
  4. [转]抛弃jQuery,使用原生JavaScript
  5. 微服务实现不同登陆_PaaS与IaaS在微服务架构实现方面的6大不同
  6. python安装pdf模块_Python PyPDF2模块安装使用解析
  7. c语言全段字符的类别,中兴LTE试题-1
  8. PLC通讯智能网关-不用PLC编程,实现西门子PLC与罗克韦尔(AB)PLC之间数据通讯
  9. 怎么利用python输出星座符号_Python输出十二星座的符号
  10. 卡尔曼滤波原理学习笔记
  11. 物联网碎片化的一些思考
  12. 豆果美食批量发布菜谱软件
  13. 精心分享8个让人大开眼界的软件和网站,极大提高办公效率
  14. webstorm html代码提示设置,Webstorm设置代码提示
  15. 本科毕业论文的引言怎么写?
  16. python音标1003python音标_python selenium 爬取百度翻译单词音标
  17. 不懂技术,怎样制作手机电子书?
  18. TypeScript 中slice(-1)是什么意思?
  19. CSS学习笔记1(2020年11月)
  20. 易基因 | 文献解读:单细胞RRBS+RNA测序揭示黄曲霉毒素B1诱导S期阻滞L02细胞肝毒性新机制

热门文章

  1. python中如何定义颜色_Python图像处理之颜色的定义与使用分析
  2. oracle locked time,Oracle里面的用户smsdb无法登录 LOCKED(TIMED)
  3. 深度学习训练中噪声减小吗_【机器学习 155】DoubleEnsemble
  4. 必须掌握的空调制冷系统维修要点
  5. 多角度对比数据中心常见的三种走线方式
  6. POE交换机应用技术知识大全
  7. easyui表单网格列错位_《HTML5从入门到精通》——第3章 HTML表格与表单
  8. 探讨计算机房的防火安全
  9. 成功解决MSB8020 The build tools for v141 (Platform Toolset = ‘v141‘) cannot be found. To build using the
  10. 成功解决AttributeError: 'list' object has no attribute 'ndim'