自动驾驶规划算法-Dijkstra

自动驾驶规划算法

Dijkstra

仅作为个人代码记录

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <time.h>

class Solution
{
//function
public:

	//Solution(){}
	Solution()//default constructor
		: m_number(0)
		, m_judgeSuccess(false)
	{
	}

	Solution(int f_number, bool f_judgeSuccess)//value transfer constructor
		: m_number(f_number)
		, m_judgeSuccess(f_judgeSuccess)
	{
	}
	Solution(Solution &f_solution)//copy constructor
	{
		this->m_number = f_solution.m_number;
		this->m_judgeSuccess = f_solution.m_judgeSuccess;
	}

	//conversion constructor : different type of pass-by-value constructor
	//...

	~Solution() {};
	
	void f_printfVectorSpace(vector<vector<int>>& f_spaceVector);
	
private:
	Solution(const Solution&) {}; //prevent copy constructor
	void operator=(const Solution&) {};//prevent copy assignment operator

//member
public:
	int  m_number;
	bool m_judgeSuccess;
	TreeNode *m_tempTreeNode;
//private:
};

int Solution::Dijkstra(vector<vector<int>>& f_path, vector<char> f_pathPoint)
{
	vector<int> l_pathDis(f_path.size(), INT_MAX);//Regard as distance from others point to start point
	vector<bool> l_pathPointUsed(f_path.size());  //Regard wherher this point has been used
	unordered_map<char, string> l_pathPointMap;   //Record the point where the shortest path passes
	l_pathPointMap[f_pathPoint.front()] = f_pathPoint.front();
	l_pathPointUsed.front() = true;
	l_pathDis.front() = 0;
	for (int i = 0; i < f_path.size(); )
	{
		int l_minPath = INT_MAX, l_minPathPoint = -1;
		for (int j = 0; j < f_path.size(); j++)
		{
			if (!l_pathPointUsed[j] && l_pathDis[i] + f_path[i][j] < l_pathDis[j] && f_path[i][j] > 0)
			{
				l_pathDis[j] = l_pathDis[i] + f_path[i][j];
				l_pathPointMap[f_pathPoint[j]] = l_pathPointMap[f_pathPoint[i]] + f_pathPoint[j];
			}
		}

		for (int k = 0; k < l_pathDis.size(); k++)
		{
			if (!l_pathPointUsed[k] && l_pathDis[k] < l_minPath)
			{
				l_minPath = l_pathDis[k];
				l_minPathPoint = k;
			}
		}
		if (-1 != l_minPathPoint)
		{
			l_pathPointUsed[l_minPathPoint] = true;
			i = l_minPathPoint;
		}
		else
		{
			break;//
		}
	}
	return 0;
}
int main()
{
	Solution l_solution;
	//Dijstra
	{
      /*       10             A  B  C  D  E  F  G 
			B --- C        A  0  12 —— —— —— 16 14
		12 / \7 6/|\ 3     B  12 0  10 —— —— 7  ——
		  /16 \ / |	\      C  —— 10 0  3  5  6  ——
		 A --- F  |5 D     D  —— —— 3  0  4  —— ——
		  \   / \ | /      E  —— —— 5  4  0  2  8 
		14 \ /9 2\|/ 4     F  16 7  6  —— 2  0  9 
			G --- E        G  14 —— —— —— 8  9  0 
		       8                                  *//*
		vector<vector<int>> l_path = {
			{0 , 12, -1, -1, -1, 16, 14},
			{12, 0 , 10, -1, -1, 7 , -1},
			{-1, 10, 0 , 3 , 5 , 6 , -1},
			{-1, -1, 3 , 0 , 4 , -1, -1},
			{-1, -1, 5 , 4 , 0 , 2 , 8 },
			{16, 7 , 6 , -1, 2 , 0 , 9 },
			{14, -1, -1, -1, 8 , 9 , 0 }
		};
		vector<char> l_pathPoint = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
		int l_minPath = l_solution.Dijkstra(l_path, l_pathPoint);
		*/
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值