题目:
下过象棋的人都知道,马只能走'日'字形(包括旋转90°的日),现在想象一下,给你一个n行m列网格棋盘,棋盘的左下角有一匹马,请你计算至少需要几步可以将它移动到棋盘的右上角,若无法走到,则输出-1.如n=1,m=2,则至少需要1步;若n=1,m=3,则输出-1。
#寻找下一步左右可能的点
def goNextStep(currentP,n,m):
result=[]
x=currentP[0]
y=currentP[1]
#分析理论8种情况
if x+1<=n and y+2<=m:
result.append([x+1,y+2])
if x+1<=n and y-2>=0:
result.append([x+1,y-2])
if x+2<=n and y-1>=0:
result.append([x+2,y-1])
if x+2<=n and y+1<=m:
result.append([x+2,y+1])
if x-1>=0 and y+2<=m:
result.append([x-1,y+2])
if x-1>=0 and y-2>=0:
result.append([x-1,y-2])
if x-2>=0 and y-1>=0:
result.append([x-2,y-1])
if x