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

最近力扣刷了一些广度优先,深度优先的题目,看了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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值