Super easy python solution


#1

class Solution:
# @param A : integer
# @param B : integer
# @param C : integer
# @param D : integer
# @param E : integer
# @param F : integer
# @return an integer

def knight(self, A, B, C, D, E, F):
    from collections import deque
    q = deque()
    q.append((C-1, D-1))
    if C==E and D==F:
        return 0
    d = [[0 for _ in range(B)] for _ in range(A)]
    mov = [(2,1), (1,2), (-2,1), (2,-1), (-1,2), (1,-2), (-2,-1), (-1,-2)]
    while q:
        p = q.popleft()
        for m in mov:
            if 0<= p[0]+m[0] <= A-1 and 0<=p[1]+m[1]<=B-1 and d[p[0]+m[0]][p[1]+m[1]] ==0:
                d[p[0]+m[0]][p[1]+m[1]] = 1 + d[p[0]][p[1]]
                if p[0]+m[0] == E-1 and p[1]+m[1] == F-1:
                    return 1 + d[p[0]][p[1]]
                q.append((p[0]+m[0], p[1]+ m[1]))
    return -1