å¨é¡¹ç®ä¸éå°äºèªå¨å¯»è·¯çéæ±ï¼äºæ¯å³å®å¼å§å¦ä¹ ä¸ä¸Aæï¼å¯¹äºAææä¹æ²¡æ深究ï¼åªè½è¯´æ¯å强æå®äºéæ±ï¼å¨è¿å大家å享ä¸ä¸ï¼ç¸äºè¿æ¥ï¼
Aææ个å
¬å¼ f(x) = g(x) + h(x)
,ææ¸
æ¥è¿ä¸ªå
¬å¼å°±å¥½åäºï¼f(x)å°±æ¯å½åä½ç½®å°ä¸ä¸ä¸ªä½ç½®çæ»ä»·å¼ï¼g(x)表示å®é
ä»·ï¼è¿æ¯è¯´è¿ä¸é¨å代价æ¯ç¡®å®çï¼h(x)表示估价å¼ï¼å°±æ¯è¯´æ
ä»ä¸ä¸ä¸ªä½ç½®å°å°ç»ç¹ç代价æ¯æªç¥çï¼æ以å«ä¼°ä»·å¼ï¼å¦å¾ä¸æ示ï¼é»è²æ ¼å表示å½åä½ç½®ï¼ç»¿è²æ ¼å表示ä¸ä¸æ¥å¯è½å°è¾¾çä½ç½®ï¼å³ä¸ãä¸ãå·¦ãå³è¿å 个æ¹
åï¼çº¢è²æ ¼å表示ç»ç¹ï¼è¤è²è¡¨ç¤ºéç¢ç©ï¼ç°å¨è¦ä»é»è²æ ¼åå°è¾¾çº¢è²æ ¼åï¼é£ä¹é»è²æ ¼åçä¸ä¸æ¥è¯å®æ¯ç»¿è²æ ¼åå½ä¸çä¸ä¸ªï¼é»è²æ ¼åå°ç»¿è²æ ¼åä¹é´æ¯ç¸æ¨ç
çï¼æ以æ们å¯ä»¥å¾æç¡®çç¥éå®çå®é
代价为1ï¼ç§»å¨ä¸æ¥ç代价ï¼å³g(x)ï¼ç»¿è²æ ¼åå°çº¢è²æ ¼åä¹é´éçå¾é¿çè·ç¦»ï¼ä¸é´è¿æéç¢ç©ï¼æ以è¿ä¸ªä»£ä»·æ¯æª
ç¥çï¼å³h(x)ï¼æ以æ»ç代价就为f(x) = g(x) +
h(x)ï¼æ们çå°å¨å´æ4个绿è²çæ ¼åï¼å°åºèµ°é£ä¸æ¥æ¯è¾å¥½å¢ï¼æ以æ们è¦æè¿4ä¸ªæ ¼åçf(x)å¼é½æ±åºæ¥ï¼ç¶åè¿è¡æåºï¼éæ©f(x)å¼æå°çï¼å³
æ»ä»£ä»·æå°çé£ä¸ªæ ¼åï¼ä»¥æ¤æ¹æ³ç»§ç»ä¸å»ï¼ç´å°å°è¾¾ç»ç¹ æè
å°å¾ä¸æ²¡æ绿è²æ ¼åäº
ä¸é¢æ¥çä¸ä¸è¿ä¸ªå·¥å
·ç±»ï¼g(x)åh(x)è¦éçæ¯è¾åéï¼ä¸è¬å°±æ¯éç¨çæ¼åé¡¿ç®æ³ï¼å³ä¸¤ç¹å¨xæ¹ååyæ¹åçè·ç¦»ä¹åï¼
-- Filename: PathUtil.lua
-- Author: bzx
-- Date: 2014-07-01
-- Purpose: 寻路
module("PathUtil", package.seeall)
local _map_data -- å°å¾æ°æ®
local _open_list -- å¼æ¾èç¹
local _open_map -- å¼æ¾èç¹ï¼ä¸ºäºæé«æ§è½èå
local _close_map -- å
³éèç¹
local _deleget -- 代ç
local _dest_point -- ç®æ ç¹
local _start_point -- èµ·ç¹
local _path -- è·¯å¾
-- 寻æ¾è·¯å¾
--[[
deleget = {
g = function(point1, point2)
-- add your code
-- è¿åç¹point1å°ç¹point2çå®é
代价
end
h = function(point1, point2)
-- add your code
-- è¿åç¹point1å°ç¹point2çä¼°ç®ä»£ä»·
end
getValue = function(j, i)
-- è¿åå°å¾ä¸ç¬¬iè¡ï¼ç¬¬jåçæ°æ® 1为éç¢ç©ï¼0为ééç¢ç©
end
width -- å°å¾å®½åº¦
height -- å°å¾é«åº¦
}
--]]
function findPath(deleget, start_point, dest_point)
_deleget = deleget
_dest_point = dest_point
_start_point = start_point
init()
while not table.isEmpty(_open_list) do
local cur_point = _open_list[1]
table.remove(_open_list, 1)
_open_map[cur_point.key] = nil
if isEqual(cur_point, dest_point) then
return makePath(cur_point)
else
_close_map[cur_point.key] = cur_point
local next_points = getNextPoints(cur_point)
for i = 1, #next_points do
local next_point = next_points[i]
if _open_map[next_point.key] == nil and _close_map[next_point.key] == nil and isObstacle(next_point) == false then
_open_map[next_point.key] = next_point
table.insert(_open_list, next_point)
end
end
table.sort(_open_list, compareF)
end
end
return nil
end
function init()
_open_list = {}
_open_map = {}
_close_map = {}
_path = {}
_map_data = {}
for i = 1, _deleget.height do
_map_data[i] = {}
for j = 1, _deleget.width do
local value = _deleget.getValue(j, i)
_map_data[i][j] = value
end
end
_open_map[getKey(_start_point)] = _start_point
table.insert(_open_list, _start_point)
end
function createPoint(x, y)
local point = {
["x"] = x,
["y"] = y,
["last"] = nil,
["g_value"] = 0,
["h_value"] = 0,
["f_value"] = 0
}
point["key"] = getKey(point)
return point
end
-- å¾å°ä¸ä¸ä¸ªå¯ä»¥ç§»å¨çç¹
-- @param point å½åæå¨ç¹
function getNextPoints(point)
local next_points = {}
for i = 1, #_deleget.directions do
local offset = _deleget.directions[i]
local next_point = createPoint(point.x + offset[1], point.y + offset[2])
next_point["last"] = point
if next_point.x >= 1 and next_point.x <= _deleget.width and next_point.y >= 1 and next_point.y <= _deleget.height then
next_point["g_value"] = _deleget.g(point, next_point)
next_point["h_value"] = _deleget.h(point, _dest_point)--math.abs(next_points.x - _dest_point.x) + math.abs(next_points.y - _dest_point.y)
next_point["f_value"] = next_point.g_value + next_point.h_value
table.insert(next_points, next_point)
end
end
return next_points
end
-- å¾å°è·¯å¾
-- @param end_point ç®æ ç¹
function makePath(end_point)
_path = {}
local point = end_point
while point.last ~= nil do
table.insert(_path, createPoint(point.x, point.y))
point = point.last
end
local start_point = point
table.insert(_path, start_point)
return _path
end
-- 两个ç¹ç代价æ¯è¾å¨
function compareF(point1, point2)
return point1.f_value < point2.f_value
end
-- æ¯å¦æ¯éç¢ç©
function isObstacle(point)
local value = _map_data[point.y][point.x]
if value == 1 then
return true
end
return false
end
-- 两个ç¹æ¯å¦æ¯åä¸ä¸ªç¹
function isEqual(point1, point2)
return point1.key == point2.key
end
-- æ ¹æ®ç¹å¾å°ç¹çkey
function getKey(point)
local key = string.format("%d,%d", point.x, point.y)
return key
end
ä¸é¢æ¯å·¥å
·ç±»PathUtilçç¨æ³
local deleget = {}
deleget.g = function(point1, point2)
return math.abs(point1.x - point2.x) + math.abs(point1.y - point2.y)
end
deleget.h = deleget.g
deleget.getValue = function(j, i)
local index = FindTreasureUtil.getIndex(j, i)
local map_info = _map_info.map[index]
if map_info.display == 0 and map_info.eid ~= 1 then
return 0
end
return 1
end
deleget.directions = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}} -- å·¦ï¼ä¸ï¼ä¸ï¼å³
deleget.width = _cols
deleget.height = _rows
local dest_row, dest_col = FindTreasureUtil.getMapPosition(tag)
local dest_point = PathUtil.createPoint(dest_col, dest_row)
local start_row, start_col = FindTreasureUtil.getMapPosition(_player_index)
local start_point = PathUtil.createPoint(start_col, start_row)
_path = PathUtil.findPath(deleget, start_point, dest_point)
_pathå°±æ¯æ们æ¾å°çè·¯å¾ï¼èµ·ç¹ä¸ºæåä¸ä¸ªå
ç´ ï¼ç»ç¹ä¸ºç¬¬ä¸ä¸ªå
ç´
温馨提示:内容为网友见解,仅供参考