【发布时间】:2017-01-25 11:54:56
【问题描述】:
计算manhattan distances的最佳方法是什么
我目前的解决方案是:
def distance(state):
target_state = (1,2,3,4,5,6,7,8,0)
target_matrix = np.reshape(np.asarray(list(target_state)),(-1,3))
reshaped_matrix = np.reshape(np.asarray(list(state)),(-1,3))
dist = 0
for i in range(1,9):
dist = dist + (abs(np.where(target_matrix == i)[0][0]
- np.where(reshaped_matrix == i)[0][0]) +
abs(np.where(target_matrix == i)[1][0]
- np.where(reshaped_matrix == i)[1][0]))
return dist
【问题讨论】:
-
一定有你没有向我们解释的事情。曼哈顿距离是
dx + dy,这也是一种非常有效的计算方法。 -
目标状态保持不变。有没有比我的方法更好的方法?
-
我绝对不会为此使用 numpy...
-
有什么更好的方法呢?
-
使用普通 Python 列表。
标签: python python-3.x numpy