#!/bin/python import sys n = int(raw_input().strip()) # your code goes here #Notes: #First, each direction needs to be able to be divisible by... #Second, the knight should never have to return to a spot it was already on; otherwise it has failed to make it #Perhaps work on sets? If at least one item in the set matches in every one of them, give a value; otherwise, -1 #If it moves, do not allow returning; if fails, return to some earlier spot from itertools import product, combinations knights = product(range(1,n),repeat=2) finish = (n-1, n-1) score = [[None for i in range(n-1)] for j in range(n-1)] def failcheck(x,y,n): if (x >= n) or (x < 0): return True elif (y >= n) or (y < 0): return True else: return False def ul(x, y, a, b, n): if failcheck(x-a,y-b,n): return [] else: return [x-a,y-b] def br(x, y, a, b, n): if failcheck(x+a,y+b,n): return [] else: return [x+a,y+b] def bl(x, y, a, b, n): if failcheck(x-a,y+b,n): return [] else: return [x-a,y+b] def ur(x, y, a, b, n): if failcheck(x+a,y-b,n): return [] else: return [x+a,y-b] for knight in list(knights): board = [[None for i in range(n)] for j in range(n)] board[0][0] = 0 a = knight[0] b = knight[1] x = 0 y = 0 moves = 1 steps = 0 fail = 0 # Set of locations: looking for (n-1, n-1) new_loc = [(0,0)] # Places: checking places step on places = set([0,0]) while moves != 0: track = [] """ if x + a > finish[0]: x -= a y += b elif y + b > finish[1]: x += a y -= b else: x += a y += b board[x][y] = steps if x == finish[0] & y == finish[1]: moves = 0 score[knight[0]][knight[1]] = moves elif steps > 10: moves = 0 score[knight[0]][knight[1]] = -1 print board """ for [x,y] in new_loc: ula = ul(x, y, a, b, n) ulb = ul(x, y, b, a, n) ura = ur(x, y, a, b, n) urb = ur(x, y, b, a, n) bla = bl(x, y, a, b, n) blb = bl(x, y, b, a, n) bra = br(x, y, a, b, n) brb = br(x, y, b, a, n) realmoves = filter(lambda x: x != (), map(tuple,[ula, ulb, ura, urb, bla, blb, bra, brb])) #print "realmoves after filter: {}".format(realmoves) #realmoves = set(realmoves) #print "realmoves after set: {}".format(realmoves) #realmoves = map(list, realmoves) #print "realmoves after mapping: {}".format(realmoves) # print realmoves track += [x for x in realmoves if ((x != ()) and (x not in places))] #print "track = {}".format(track) places.update([x for x in track]) #print "places = {}".format(places) for pl in new_loc: board[pl[0]][pl[1]] = steps if finish in new_loc: moves = 0 score[knight[0]-1][knight[1]-1] = steps #print "Finish!!" break if not track and finish not in new_loc: moves = 0 score[knight[0]-1][knight[1]-1] = -1 new_loc = track steps += 1 #print "new_loc = {}".format(track) #print "score = {}".format(score) #print new_loc for row in score: for y in row: print "{}".format(y), print