Knight_Sol2
unknown
plain_text
a year ago
908 B
3
Indexable
Never
def moveNight(a, b, n, loc): for dx,dy in [[a,b], [b,a]]: for sx, sy in [[1,1], [-1,1], [1,-1], [-1,-1]]: x = loc[0] + dx * sx y = loc[1] + dy * sy if any([x<0, y<0, x>n-1, y>n-1]): # Out of bounds continue yield x,y def breadthF(a, b, n): traveled = set(((0,0),)) level = set(((0,0),)) d = 1 while level: newLevel = set() for loc in level: for x,y in moveNight(a,b,n,loc): if (x,y) == (n-1,n-1): return d if (x,y) not in traveled: traveled.add((x,y)) newLevel.add((x,y)) level = newLevel d += 1 return -1 def knightlOnAChessboard(n): # Loop rows and columns ans = [[breadthF(row,col,n) for col in range(1,n)] for row in range(1, n)] return ans