You are viewing a single comment's thread. Return to all comments →
Python 3
d=[[0 for j in range(26)] for i in range(26)] visit=[[False for j in range(26)] for i in range(26)] def BFS_at(x, y, a, b, n): queue=[] queue.append([x, y]) move_X=[a, a, -a, -a, b, b, -b, -b] move_Y=[b, -b, b, -b, a, -a, a, -a] visit[x][y]=True while len(queue)>0: now_x=queue[0][0] now_y=queue[0][1] queue.pop(0) if now_x==n-1 and now_y==n-1: return for i in range(len(move_X)): m_X=now_x+move_X[i] m_Y=now_y+move_Y[i] if m_X<0 or m_X>n-1: continue if m_Y<0 or m_Y>n-1: continue if visit[m_X][m_Y] is False: visit[m_X][m_Y]=True queue.append([m_X, m_Y]) d[m_X][m_Y]=d[now_x][now_y]+1 def knightlOnAChessboard(n): result=[[0 for i in range(n-1)] for j in range(n-1)] for a in range(1, n): for b in range(1, n): for i in range(26): for j in range(26): d[i][j]=0 visit[i][j]=False BFS_at(0, 0, a, b, n) if visit[n-1][n-1]: result[a-1][b-1]=d[n-1][n-1] else: result[a-1][b-1]=-1 return result
Seems like cookies are disabled on this browser, please enable them to open this website
KnightL on a Chessboard
You are viewing a single comment's thread. Return to all comments →
Python 3