Advertisement
Guest User

Super Knight

a guest
Oct 31st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. import sys
  2. from collections import deque
  3. import time
  4.  
  5. M = int(sys.argv[1])
  6. N = int(sys.argv[2])
  7. p = int(sys.argv[3])
  8. q = int(sys.argv[4])
  9. X = int(sys.argv[5])
  10. Y = int(sys.argv[6])
  11. U = int(sys.argv[7])
  12. V = int(sys.argv[8])
  13. move = zip([-p,p,-p,p,-q,-q,q,q],[-q,-q,q,q,-p,p,-p,p])
  14.  
  15. if 2 > M or N > 100:
  16.     print("Constraint broken\n")
  17.     print("2 <= M and N <= 100\n")
  18.     exit(0)
  19.  
  20. if 1 > X or X > M or 1 > Y or Y > N:
  21.     print ("Constraint broken\n")
  22.     print ("1 <= X <= M, 1 <= Y <= N\n")
  23.     exit(0)
  24.  
  25. if 1 > U or U > M or 1 > V or V > N:
  26.     print ("Constraint broken\n")
  27.     print ("1 <= U <= M, 1 <= V <= N\n")
  28.     exit(0)
  29.  
  30. # Move to a 0 based coordinate system
  31. X = X - 1
  32. Y = Y - 1
  33. U = U - 1
  34. V = V - 1
  35.  
  36. board = [[[] for x in range(N)] for y in range(M)]
  37.  
  38. def printBoard():
  39.     print "   ",
  40.     for i in range(N):
  41.         print i,
  42.     print ""
  43.     print "_"*((N*2)+4)
  44.     for i in range(M):
  45.         print i, "|",
  46.         for j in range(N):
  47.             print len(board[i][j]),
  48.         print "\n"
  49.     print "\n"
  50.  
  51. def insideBoard(x, y):
  52.     return x >= 0 and x < M and y >= 0 and y < N
  53.  
  54. def depthFirstSearch(fromX, fromY, targetX, targetY, path):
  55.     board[fromX][fromY] = path
  56.    
  57.     if fromX == targetX and fromY == targetY:
  58.         return
  59.     for (x,y) in move:
  60.         x = fromX+x
  61.         y = fromY+y
  62.         if insideBoard(x, y) and (len(board[x][y]) == 0 or (len(path) + 1) < len(board[x][y])):
  63.             newPath = path[:]
  64.             newPath.append((x, y))
  65.             depthFirstSearch(x, y, targetX, targetY, newPath)
  66.  
  67. def breadthFirstSearch(fromX, fromY, targetX, targetY, path):
  68.     toSearch = []
  69.    
  70.     for (x, y) in move:
  71.         x = fromX+x
  72.         y = fromY+y
  73.         if insideBoard(x, y) and (len(board[x][y]) == 0 or (len(path)+1) < len(board[x][y])):
  74.             newPath = path[:]
  75.             newPath.append((x, y))
  76.             toSearch.append(((x, y), newPath))
  77.  
  78.  
  79.     while True:
  80.         ((fromX, fromY), path) = toSearch.pop(0)
  81.         board[fromX][fromY] = path
  82.        
  83.         if fromX == targetX and fromY == targetY:
  84.             break
  85.        
  86.         for (x, y) in move:
  87.             x = fromX+x
  88.             y = fromY+y
  89.             if insideBoard(x, y) and (len(board[x][y]) == 0 or (len(path)+1) < len(board[x][y])):
  90.                 newPath = path[:]
  91.                 newPath.append((x, y))
  92.                 toSearch.append(((x, y), newPath))
  93.  
  94.         if len(toSearch) == 0:
  95.             break
  96.  
  97. board = [[[] for x in range(N)] for y in range(M)]
  98. t0 = time.time()
  99. breadthFirstSearch(X, Y, U, V, [(X, Y)])
  100. t1 = time.time()
  101. printBoard()
  102. print board[U][V]
  103. print "Time:", t1-t0
  104.  
  105. board = [[[] for x in range(N)] for y in range(M)]
  106. t0 = time.time()
  107. depthFirstSearch(X, Y, U, V, [(X, Y)])
  108. t1 = time.time()
  109. printBoard()
  110. print board[U][V]
  111. print "Time:", t1-t0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement