Recent Posts
None | 21 sec ago
C++ | 35 sec ago
C | 36 sec ago
None | 47 sec ago
None | 49 sec ago
Lua | 1 min ago
None | 1 min ago
mIRC | 1 min ago
None | 1 min ago
C++ | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Jonathan on the 1st of Sep 2009 09:40:42 AM Download | Raw | Embed | Report
  1. # Last Piece Puzzle solving program
  2.  
  3.  
  4. import numpy, matplotlib, pylab
  5.  
  6. pieces = [
  7.     numpy.matrix([[1,-1,1,-1],[0,0,0,1]]),
  8.     numpy.matrix([[1,-1,1,-1],[-1,0,0,0]]),
  9.     numpy.matrix([[-1,1,0],[0,-1,0],[0,1,-1]]),
  10.     numpy.matrix([[-1,0],[1,-1],[0,1]]),
  11.     numpy.matrix([[0,-1,1],[0,1,0],[1,-1,0]]),
  12.     numpy.matrix([[-1,1,-1,1],[0,-1,0,0]]),
  13.     numpy.matrix([[1,0,0],[-1,1,-1]]),
  14.     numpy.matrix([[0,-1,0],[-1,1,-1]]),
  15.     numpy.matrix([[1,0,0],[-1,1,0],[0,-1,1]]),
  16.     numpy.matrix([[1,-1,1,0],[0,0,-1,1]]),
  17.     numpy.matrix([[1,-1,1],[-1,0,0]]),
  18.     numpy.matrix([[0,0,1,-1],[-1,1,-1,0]]),
  19.     numpy.matrix([[0,1,0,0],[1,-1,1,-1]])
  20.     ]
  21.  
  22. board = numpy.zeros([8,8], dtype=int)
  23. board[6,6]=1
  24. board[7,6]=1
  25. board[7,7]=1
  26.  
  27. positions = [(0,0) for piece in pieces] # starting positions of pieces
  28. rotations = [0 for piece in pieces] # n of counterclockwise rotations [0..3]
  29. placed = [False for piece in pieces]
  30. curpiece = 0
  31.  
  32. def placePiece(curpiece):
  33.     """Returns true if piece placed correctly, False if not valid position.
  34.    Doesn't check boundaries."""
  35.     # check inouty
  36.     piece = pieces[curpiece]
  37.     position = positions[curpiece]
  38.     for x in range(piece.shape[0]):
  39.         for y in range(piece.shape[1]):
  40.             if (piece[x,y] != 0 and
  41.                 not inouty(position[0]+x, position[1]+y) == piece[x,y]
  42.                 ):                
  43.                 return False
  44.             if piece[x,y] != 0 and board[position[0]+x, position[1]+y] == 1:
  45.                 return False
  46.     return True
  47.  
  48. def incpos(curpiece):
  49.     while True:
  50.         if rotations[curpiece] < 3:
  51.             rotations[curpiece] += 1
  52.             pieces[curpiece] = numpy.rot90(pieces[curpiece])
  53.         elif 8-positions[curpiece][1]-min(pieces[curpiece].shape)>0:
  54.             positions[curpiece] = (positions[curpiece][0], positions[curpiece][1]+1)
  55.             rotations[curpiece] = 0
  56.             pieces[curpiece] = numpy.rot90(pieces[curpiece])
  57.         elif 8-positions[curpiece][0]-min(pieces[curpiece].shape)>0:
  58.             positions[curpiece] = (positions[curpiece][0]+1, 0)
  59.             rotations[curpiece] = 0
  60.             pieces[curpiece] = numpy.rot90(pieces[curpiece])
  61.         else:
  62.             return False
  63.         # break if position valid
  64.         if (8-positions[curpiece][1]-pieces[curpiece].shape[1]>=0 and
  65.             8-positions[curpiece][0]-pieces[curpiece].shape[0]>=0):
  66.             return True
  67.  
  68. def inouty(x,y):
  69.     """+1 if male square, -1 if female square. Anomalous square hard-coded."""
  70.     return ((x+y+1)%2)*2-1
  71.  
  72. def tobin(piece, position):
  73.     n=0L
  74.     for x in range(piece.shape[0]):
  75.         for y in range(piece.shape[1]):
  76.             if piece[x,y] != 0:
  77.                 n = n | (2**((x+position[0])*8+y+position[1]))
  78.     return n
  79.  
  80. def frombin(piece, n):
  81.     mat = numpy.zeros([8,8],int)
  82.     for s in range(8**2):
  83.         if n & 2**s:
  84.             mat[s // 8, s % 8] = piece+1
  85.     return mat
  86.  
  87. allpositions = []
  88. allpositions_cache = set()
  89. for i in range(len(pieces)):
  90.     if placePiece(i):
  91.         n=tobin(pieces[i], positions[i])
  92.         allpositions.append((i, n))
  93.         allpositions_cache.add(n)
  94.     while incpos(i):
  95.         if placePiece(i):
  96.             n=tobin(pieces[i], positions[i])
  97.             if not n in allpositions_cache:
  98.                 allpositions.append((i, n))
  99.                 allpositions_cache.add(n)
  100.  
  101. fixedpiece = tobin(board, (0,0))
  102.  
  103. allpositions_sub = dict([(k, []) for k in range(64)])
  104. for piece in allpositions:
  105.     for n in range(64)[::-1]:
  106.         if 2**n & piece[1]:
  107.             allpositions_sub[n].append(piece)
  108.             break
  109.  
  110. def findSolution(boardstate, usedpieces, usedpiecenums, uptosquare):
  111.     if len(usedpieces) == len(pieces):
  112.         return [usedpieces]
  113.     if boardstate & 2**uptosquare:
  114.         return findSolution(boardstate, usedpieces, usedpiecenums, uptosquare - 1)
  115.     else:
  116.         results = []
  117.         for piece in allpositions_sub[uptosquare]:
  118.             if uptosquare == 8**2 -1:
  119.                 print ".",
  120.             if (boardstate & piece[1]) == 0 and not piece[0] in usedpiecenums:
  121.                 result = findSolution(
  122.                     boardstate = boardstate | piece[1],
  123.                     usedpieces = usedpieces + [piece],
  124.                     usedpiecenums = usedpiecenums + [piece[0]],
  125.                     uptosquare = uptosquare-1)
  126.                 if result != []:
  127.                     results.extend(result)
  128.         return results
  129.  
  130. print "Initial setup complete. Searching..."
  131. solutions = findSolution(fixedpiece, [], [], 8**2-1)
  132.  
  133. print "Possible solutions:"
  134.  
  135. for solution in solutions:
  136.     print sum([frombin(*piecet) for piecet in solution])
  137.  
  138. n=0
  139. pylab.spectral()
  140. for solution in solutions:
  141.     pylab.subplot(3,2,n+1)
  142.     pylab.matshow(sum([frombin(*piecet) for piecet in solution]),fignum=0)
  143.     pylab.colorbar()
  144.     n += 1
  145. pylab.show()
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: