Advertisement
Guest User

Untitled

a guest
Apr 8th, 2013
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # knightstour.py
  2. #
  3. # created by: M. Peele
  4. # section: 01
  5. #
  6. # This program implements a brute-force solution for the Knight's tour problem
  7. # using a recursive backtracking algorithm. The Knight's tour is a chessboard
  8. # puzzle in which the objective is to find a sequence of moves by the knight in
  9. # which it visits every square on the board exactly one. It uses a 6x6 array for
  10. # the chessboard where each square is identified by a row and column index, the
  11. # range of which both start at 0. Let the upper-left square of the board be the
  12. # row 0 and column 0 square.
  13. #
  14. # Imports the necessary modules.
  15.  
  16. # Main driver function which starts the recursion.
  17. def main():
  18.     global chessBoard
  19.     for row in range(6):
  20.         for col in range(6):
  21.             chessBoard = [[None for i in range(6)] for j in range(6)]
  22.             knightsTour(row, col, 1)
  23.             print()
  24.  
  25. # Recursive function that solves the Knight's Tour problem.    
  26. def knightsTour(row, col, move):
  27.     # Checks if the given index is in range of the array and is legal.
  28.     if _inRange(row, col) and _isLegal(row, col):
  29.         chessBoard[row][col] = move # Sets a knight-marker at the given index.
  30.         # If the chessBoard is full, returns True and the solved board.
  31.         if _isFull(chessBoard):
  32.             return True, _draw(chessBoard)    
  33.  
  34.         # Checks to see if the knight can make another move. If so, makes that
  35.         # move by calling the function again.
  36.         possibleOffsets = ((-2, -1), (-2, 1), (-1, 2), (1, 2), \
  37.                            (2, 1), (2, -1), (1, -2), (-1, -2))
  38.         for offset in possibleOffsets:
  39.             if knightsTour(row + offset[0], col + offset[1], move + 1):
  40.                 return True
  41.         # If the loop terminates, no possible move can be made. Removes the
  42.         # knight-marker at the given index.
  43.         chessBoard[row][col] = None
  44.         return False
  45.     else:
  46.         return False
  47.  
  48. # Determines if the given row, col index is a legal move.
  49. def _isLegal(row, col):
  50.     if _inRange(row, col) and chessBoard[row][col] == None:
  51.         return True
  52.     else:
  53.         return False
  54.  
  55. # Determines if the given row, col index is in range.
  56. def _inRange(row, col):
  57.     try:
  58.         chessBoard[row][col]
  59.         return True
  60.     except IndexError:
  61.         return False
  62.  
  63. # A solution was found if the array is full, meaning that every element in the
  64. # array is filled with a number saying the knight has visited there.
  65. def _isFull(chessBoard):
  66.     for row in chessBoard:
  67.         for col in row:
  68.             if col is None:
  69.                 return False
  70.     return True
  71.  
  72. # Draws a pictoral representation of the array.
  73. def _draw(chessBoard):
  74.     for row in chessBoard:
  75.         for col in row:
  76.             print("%4s" % col, end = " ")
  77.         print()
  78.  
  79. # Calls the main function.
  80. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement