1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| import time
open_list = [] close_list = []
class Position: x = 0 y = 0
def __init__(self, X, Y): self.x, self.y = X, Y
def __eq__(self, other): if (other and self): return self.x == other.x and self.y == other.y else: return not (self or other)
class Msg: G = 0 H = 0 IsUnk = 1 parent = None
def __init__(self): self.G = 0 self.H = 0 self.IsUnk = 1
def GetF(self): return self.G + self.H
class Board: mapx = []
def __init__(self, mapp, target_position): for i in range(len(mapp)): self.mapx.append([]) for j in range(len(mapp[0])): self.mapx[i].append( Msg()) self.mapx[i][j].IsUnk = 1 - mapp[i][j] self.mapx[i][j].H = 10 * (abs(target_position.x - i) + abs(target_position.y - j))
def GetMsg(self, pos): return self.mapx[pos.x][pos.y]
def IsInBoard(i, j): if (i >= 0 and i < len(mapp) and j >= 0 and j < len(mapp[i]) and mapp[i][j] == 0): return 1 else: return 0
def SearchPath(mapp, start_position, target_position): start_time = time.time() board = Board(mapp, target_position) board.GetMsg(start_position).IsUnk = 0 open_list.append(start_position) while (open_list != []): current_position = open_list[0] open_list.remove(current_position) if (current_position == target_position): print("成功找到解") tmp = [] while (current_position != None): tmp.append(current_position) current_position = board.GetMsg(current_position).parent tmp.reverse() for i in tmp: print(str(i.__dict__)) end_time = time.time() print(end_time - start_time) return
for i in [current_position.x - 1, current_position.x + 1]: for j in [current_position.y - 1, current_position.y + 1]: if (IsInBoard(i, j)): new_G = board.GetMsg(current_position).G + 14 if (board.mapx[i][j].IsUnk): board.mapx[i][j].IsUnk = 0 open_list.append(Position(i, j)) board.mapx[i][j].parent = current_position board.mapx[i][j].G = new_G
if (board.mapx[i][j].G > new_G): board.mapx[i][j].parent = current_position board.mapx[i][j].G = new_G j = current_position.y for i in [current_position.x - 1, current_position.x + 1]: if (IsInBoard(i, j)): new_G = board.GetMsg(current_position).G + 10 if (board.mapx[i][j].IsUnk): board.mapx[i][j].IsUnk = 0 open_list.append(Position(i, j)) board.mapx[i][j].parent = current_position board.mapx[i][j].G = new_G
if (board.mapx[i][j].G > new_G): board.mapx[i][j].parent = current_position board.mapx[i][j].G = new_G i = current_position.x for j in [current_position.y - 1, current_position.y + 1]: if (IsInBoard(i, j)): new_G = board.GetMsg(current_position).G + 10 if (board.mapx[i][j].IsUnk): board.mapx[i][j].IsUnk = 0 open_list.append(Position(i, j)) board.mapx[i][j].parent = current_position board.mapx[i][j].G = new_G
if (board.mapx[i][j].G > new_G): board.mapx[i][j].parent = current_position board.mapx[i][j].G = new_G open_list.sort(key=lambda elem: board.GetMsg(elem).GetF())
if __name__ == "__main__": mapp = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] start_position = Position(0, 0) target_position = Position(5, 6) SearchPath(mapp, start_position, target_position)
|