自动驾驶规划算法
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);
*/
}
}