物流行业受成本、中转仓、时间等因素的限制,往往需要对货品的路由线路提出很多要求,怎样快速精准的找到这样的线路,并使用计算机语言实现出来?
根据相关行业经验,抽取了以下计算模型,该算法效率不是最高的,但是比Dijkstra、链路向量等专业算法可能更容易理解:
单向路由解递归实现:
/// /// 单向路由算法 /// /// 起点 /// 终点 /// 地图 /// 限制路由距离 /// public static List > GetAllRouteLine(int start, int end, Dictionary
> map, int LIMIT) { //已找到的通路 var result = new List
>(); //类似于深度优先遍历法,回到原点遍历完成 //纵横栈-->未处理的各层节点 var stackstack = new Stack >(); //纵向栈-->已连通路段 var verstack = new Stack (); //起点可1步直达的节点列表 var nextNodes = GetNextNodes(start, verstack.ToList(), LIMIT, end, map, result); if (nextNodes != null) { //起点 verstack.Push(start); //起点对应的下一层 stackstack.Push(nextNodes); while (stackstack.Count > 0) { //当前处理层 var horqueue = stackstack.Peek(); //层层压入(待处理的层) while (horqueue.Count > 0) { nextNodes = GetNextNodes(horqueue.Peek(), verstack.ToList(), LIMIT, end, map, result); if (nextNodes != null) { verstack.Push(horqueue.Peek()); stackstack.Push(nextNodes); horqueue = stackstack.Peek(); } else { horqueue.Dequeue(); } } //层层弹出(处理完的层) while (stackstack.Peek().Count == 0) { stackstack.Pop(); verstack.Pop(); if (stackstack.Count==0) { break; } if (stackstack.Peek().Count > 0) { stackstack.Peek().Dequeue(); } } } } return result; }
/// /// 获取当前点能走通的下一点集合 /// /// 当前点 /// 已走通路段 /// 限制路由距离 /// 终点 /// 地图 /// 已完成线路集合 /// private static Queue GetNextNodes(int current, List hadNode, int LIMIT, int end, Dictionary