Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- moves = []
- def setmoves(i, j):
- global moves
- moves = []
- moves.append((i,j))
- moves.append((-i,j))
- moves.append((i,-j))
- moves.append((-i,-j))
- moves.append((j,i))
- moves.append((-j,i))
- moves.append((j,-i))
- moves.append((-j,-i))
- def id(row, col, sz):
- return row*sz + col
- def check(u,a,b,n):
- if ((u[0] + a >= 0) and (u[0] +a < n) and (u[1] + b >= 0) and (u[1] + b < n)):
- # if (abs(a) == 3 and abs(b) == 3):
- # print u, a, b, n
- return 1
- return 0
- #print par
- def bfs(s, n):
- global par
- par = [i for i in xrange(n*n)]
- visited = [0]*(n*n)
- queue = []
- queue.append(s)
- while(queue != []):
- u = queue.pop(0)
- for i,j in moves:
- if (check(u,i,j,n) and visited[id(u[0], u[1], n) + id(i,j,n)] == 0):
- visited[id(u[0], u[1], n) + id(i,j,n)] = 1
- par[id(u[0], u[1], n) + id(i,j,n)] = id(u[0], u[1], n)
- queue.append((u[0] + i, u[1] + j))
- return par
- count = 0
- def printpath(par, n):
- if (par[n] == n):
- return
- global count
- if (n == 0):
- # print 0
- return
- # print n
- count += 1
- printpath(par, par[n])
- t = int(raw_input())
- par = [i for i in xrange(t*t)]
- def main():
- global t
- global count
- for i in range(1, t):
- for j in range(1, t):
- setmoves(i,j)
- # print moves
- p = bfs((0,0),t)
- # print p
- printpath(p, t*t-1)
- if (count):
- print count,
- else:
- print -1,
- count = 0
- print ""
- main()
Add Comment
Please, Sign In to add comment