问题:
给定输入(x,y),在编号为0-63的棋盘上找到骑士从x到y的最短路径(第1行:0-8,第2行:9-15,等等)。在
我开始用Numpy来解决这个问题,但是1。结果我不允许使用Numpy,还有2。我的代码变得太复杂了。必须有更简单的方法来解决这个问题。在
以下是我的(未完成的)代码,它处理BFS搜索的前两个步骤:import numpy as np
a = np.array([[0,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]])
def answer(src, dest):
''' Takes integer inputs, converts them to board coordinates '''
x = np.argwhere(a==src)
y = np.argwhere(a==dest)
z = [tuple(l) for l in x]
''' Returns all possible moves from starting position, in coordinates '''
def knig