#!/bin/python import sys from Queue import * n = int(raw_input().strip()) #perform validation def validatePos(pos): row = pos[0] col = pos[1] if (row >= n or row < 0): return False elif (col >= n or col < 0): return False else: return True #compute the shortestPath def shortestPathBFS(row,col): #initializaitons observed = set() Q = Queue() startPos = (0,0) endPos = (n-1,n-1) #Adding the source to the queue Q.put(startPos+(0,)) #add it to visited temp_node = (startPos[0],startPos[1]) observed.add(temp_node) #run the loop to find the shortest Path while (not Q.empty()): #Get the first Node startPos = Q.get() #Get it's level level = startPos[2] #Perform calculations for 8 positions pos = list() pos.append( (startPos[0] + row, startPos[1] + col ) ) pos.append( ( startPos[0] - row, startPos[1] - col ) ) pos.append( ( startPos[0] + row, startPos[1] - col ) ) pos.append( ( startPos[0] - row, startPos[1] + col ) ) pos.append( ( startPos[0] + col, startPos[1] + row ) ) pos.append( ( startPos[0] - col, startPos[1] - row ) ) pos.append( ( startPos[0] + col, startPos[1] - row ) ) pos.append( ( startPos[0] - col, startPos[1] + row ) ) #Validate if the positions are correct for index in xrange(8): if ( validatePos(pos[index]) ): if ( pos[index] not in observed ): #Check if the node is the endPos if (pos[index] == endPos): return level + 1 #add it to observed set observed.add(pos[index]) #Add this to the queue temp_node = pos[index] + (level + 1,) Q.put(temp_node) return -1 #print values for row in xrange(1,n): for col in xrange(1,n): value = shortestPathBFS(row,col) print '{}'.format(value), print ""