import numpy as npclass State def __init__self state directionFlag=None parent=None f=0 selfstate = state selfdirection = up down right left if directionFlag selfdir
A* 迭代
def solve(self): openTable = [self] closeTable = [] while openTable: # 获取F值最小的点 curState = openTable.pop(0) # 加入close表 closeTable.append(curState) # 获取下一步的点 subStates = curState.nextStep() for state in subStates: # 判断是否已经在close表中 if state in closeTable: continue # 判断是否已经在open表中 if state not in openTable: # 判断是否和最终结果相同 if (state.state == state.answer).all(): path = [state] while state.parent and state.parent != originState: path.append(state.parent) state = state.parent path.reverse() return path openTable.append(state) else: # 更新F值 index = openTable.index(state) if state.f < openTable[index].f: openTable[index].f = state.f openTable[index].parent = state.parent # open表按F值进行排序 openTable.sort(key=compareNum) return None, Non
原文地址: http://www.cveoy.top/t/topic/hbNK 著作权归作者所有。请勿转载和采集!