--到地图去的关系表
local tbToMap = {}
tbToMap[1000] = {1001,1003,1010}
tbToMap[1001] = {1000,1002,1004,1009}
tbToMap[1002] = {1005,1001,1009}
tbToMap[1003] = {1000,1006,1004}
tbToMap[1004] = {1007,1001,1003,1005}
tbToMap[1005] = {1002,1004,1011}
tbToMap[1006] = {1003,1007}
tbToMap[1007] = {1004,1006,1008}
tbToMap[1008] = {1007}
tbToMap[1009] = {1001,1002,1010}
tbToMap[1010] = {1009,1000}
tbToMap[1011] = {1005}
local tbResult = {}--所有遍历结果
local FIRID = 1008
local LASID = 1002
local tbGoBy = {}--走过的路
local tbBlindAlley = {}--死路节点
local num = 0
local maxRecursionNum = 100--最大递归上限
local g_findOver = false
local function findMapPath(_firstId,_lastId)
num = num + 1
if num > maxRecursionNum then--递归超过上限
return
end
print(_firstId, "_firstId = ")
tbGoBy[_firstId] = true
table.insert(tbResult, _firstId)
if _firstId == _lastId then
return
else
local dt = tbToMap[_firstId]
for k, v in ipairs(dt) do
if not tbGoBy[v] then
local value = findMapPath(v, _lastId)
print(value, "value = ")
if value == nil then
return
end
end
end
--if not g_findOver then
tbBlindAlley[_firstId] = true
--end
return _firstId
end
end
--findMapPath(1008,1010)
findMapPath(1006,1009)
--findMapPath(1006,1002)
dump(tbBlindAlley, "tbBlindAlley = ")
print(num, "num = ")
dump(tbResult, "tbResult = ")
for i = #tbResult, 1, -1 do
local mapId = tbResult[i]
if tbBlindAlley[mapId] then
table.remove(tbResult, i)
end
end
dump(tbResult, "tbResult = ")
寻找图的路径之寻路算法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本机环境centos6.8 + python2.7 + tensorflow0.11 下载tensorflow源码...