pythonの質問です。
8パズルのA*探索によるソースコードを書いています。
一応はできたのですが、実行結果が横一列になってしまいます。
これを縦一列に直すにはどのようにコードを書き換えたらいいのでしょうか?
発生している問題・エラーメッセージ
[[5, 4, 2, 6, 7, 0, 8, 1, 3], [5, 4, 2, 6, 0, 7, 8, 1, 3], [5, 4, 2, 0, 6, 7, 8, 1, 3], [5, 4, 2, 8, 6, 7, 0, 1, 3], [5, 4, 2, 8, 6, 7, 1, 0, 3], [5, 4, 2, 8, 0, 7, 1, 6, 3], [5, 4, 2, 8, 7, 0, 1, 6, 3], [5, 4, 2, 8, 7, 3, 1, 6, 0], [5, 4, 2, 8, 7, 3, 1, 0, 6], [5, 4, 2, 8, 0, 3, 1, 7, 6], [5, 4, 2, 0, 8, 3, 1, 7, 6], [5, 4, 2, 1, 8, 3, 0, 7, 6], [5, 4, 2, 1, 8, 3, 7, 0, 6], [5, 4, 2, 1, 0, 3, 7, 8, 6], [5, 0, 2, 1, 4, 3, 7, 8, 6], [0, 5, 2, 1, 4, 3, 7, 8, 6], [1, 5, 2, 0, 4, 3, 7, 8, 6], [1, 5, 2, 4, 0, 3, 7, 8, 6], [1, 0, 2, 4, 5, 3, 7, 8, 6], [1, 2, 0, 4, 5, 3, 7, 8, 6], [1, 2, 3, 4, 5, 0, 7, 8, 6], [1, 2, 3, 4, 5, 6, 7, 8, 0]]
該当のソースコード
python
1from heapq import heappush, heappop 2from pandas.core.panel import Panel 3 4class sBoard(): 5 6 def __init__(self, board_list, distance, parent): 7 self._array = board_list 8 self.heuristic = calc_heuristic(self._array) 9 self.distance = distance 10 self.cost = self.distance + self.heuristic 11 self.parent = parent 12 self.hashvalue = hash(tuple(self._array)) 13 14 def _getsBoard(self): 15 return self._array 16 17 def __hash__(self): 18 return self.hashvalue 19 20 def __eq__(self,other): 21 return self._array == other._array 22 23 def __lt__(self, other): 24 return self._array < other._array 25 26def astar(): 27 queue = [] 28 dist_dic = {} 29 visited = {} 30 31 start = sBoard(panel_list, 0, None) 32 end = sBoard(goal, 99, None) 33 34 heappush(queue, (start.cost, start)) 35 No = 0 36 37 while len(queue) > 0: 38 No += 1 39 40 now_tuple = heappop(queue) 41 now_board = now_tuple[1] 42 if now_board._array == goal or now_board._array == goal2: 43 end = now_board 44 break 45 46 index = now_board._array.index(0) 47 x, y = XY_coord(index) 48 coord_next_OK = coord_next(x, y) 49 50 for coord in coord_next_OK: 51 next_board = now_board._array[:] 52 next_index = coord[0] * No_XY + coord[1] 53 next_board[index],next_board[next_index] = next_board[next_index],next_board[index] 54 55 new_sboard = sBoard(next_board, now_board.distance+1, now_board) 56 new_distance = new_sboard.cost 57 if tuple(new_sboard._array) not in visited or \ 58 new_distance < dist_dic[new_sboard]: 59 60 dist_dic[new_sboard] = new_distance 61 visited[tuple(new_sboard._array)] = new_sboard 62 new_sboard.parent = now_board 63 heappush(queue, (new_sboard.cost, new_sboard)) 64 var = end 65 sol=[] 66 while var != start: 67 sol = sol + [var._getsBoard()] 68 var = var.parent 69 70 sol = sol + [var._getsBoard()] 71 sol.reverse() 72 73 return sol 74 75def calc_heuristic(array): 76 board_list = array 77 same = 0 78 manhattan = 0 79 for var in board_list: 80 81 x, y = XY_coord(var) 82 if goal.index(var) != board_list.index(var): 83 same += 1 84 85 pos = goal.index(var) 86 goal_board_x, goal_board_y = XY_coord(pos) 87 x, y = XY_coord(board_list.index(var)) 88 manhattan += abs(x-goal_board_x) + abs(y-goal_board_y) 89 90 heuristic = manhattan 91 92 return 1.0 * heuristic 93 94def coord_next(x, y): 95 96 coord_next_OK = [[x, y]] 97 98 if(x+1 < No_XY): 99 coord_next_OK.append([x+1, y]) 100 101 if(x-1 >= 0): 102 coord_next_OK.append([x-1, y]) 103 104 if(y-1 >= 0): 105 coord_next_OK.append([x, y-1]) 106 107 if(y+1 < No_XY): 108 coord_next_OK.append([x, y+1]) 109 110 return coord_next_OK 111 112def XY_coord(index): 113 x = index // No_XY 114 y = index % No_XY 115 return x,y 116 117def main(): 118 global panel_list, goal, goal2, No_XY 119 120 No_XY = 3 121 goal = [1, 2, 3, 4, 5, 6, 7, 8, 0] 122 goal2 = [1, 2, 3, 4, 5, 6, 8, 7, 0] 123 panel_list = [5, 4, 2, 6, 7, 0, 8, 1, 3] 124 125 sol = astar() 126 return sol 127 128if __name__ == '__main__': 129 sol = main() 130 print(sol) 131
試したこと
while文を使って縦に表示しようと思ったが、入れたところで毎回エラーが出るためわからなかったです。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー