Advertisement
Guest User

Machinarium puzzle solution

a guest
Mar 30th, 2013
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. import time
  2. import copy
  3.  
  4. class Puzzle:
  5.     history = []
  6.     LEFT = 'L'
  7.     RIGHT = 'R'
  8.     UP = 'U'
  9.     DOWN = 'D'
  10.     max = []
  11.     matrix = []
  12.    
  13.     def __init__(self,matrix):
  14.         self.solutions = []
  15.         self.history = []
  16.         self.matrix = copy.deepcopy(matrix)
  17.         self.max = [len(self.matrix) - 1, len(self.matrix[0]) - 1]
  18.    
  19.     def __repr__(self):
  20.         return str(self.matrix)
  21.    
  22.     def getPossibleDirections(self):
  23.         directions = [];
  24.         for direction in [self.UP,self.DOWN,self.LEFT,self.RIGHT]:
  25.             if self.canGo(direction):
  26.                 directions.append(direction)
  27.         return directions  
  28.    
  29.     def isCompleted(self):
  30.         for line in self.matrix:
  31.             for element in line:
  32.                 if(element == 0):
  33.                     return 0
  34.         return 1
  35.    
  36.     def canGo(self,direction):
  37.         if direction == self.UP:
  38.             result = ((self.pos[0] - 1) >= 0) and (self.matrix[self.pos[0] - 1][self.pos[1]] == 0)
  39.         elif direction == self.DOWN:
  40.             result = ((self.pos[0] + 1) <= self.max[0]) and (self.matrix[self.pos[0] + 1][self.pos[1]] == 0)
  41.         elif direction == self.LEFT:
  42.             result = ((self.pos[1] - 1) >= 0) and (self.matrix[self.pos[0]][self.pos[1] - 1] == 0)
  43.         elif direction == self.RIGHT:
  44.             result = ((self.pos[1] + 1) <= self.max[1]) and (self.matrix[self.pos[0]][self.pos[1] + 1] == 0)
  45.         return result  
  46.        
  47.     def go(self,direction):
  48.         self.history.append(direction)
  49.         while self.canGo(direction):
  50.             if direction == self.UP:
  51.                 self.pos[0] -= 1
  52.             elif direction == self.DOWN:
  53.                 self.pos[0] += 1
  54.             elif direction == self.LEFT:
  55.                 self.pos[1] -= 1
  56.             elif direction == self.RIGHT:
  57.                 self.pos[1] += 1
  58.             self.matrix[self.pos[0]][self.pos[1]] = 1          
  59.    
  60.     def branch(self):
  61.         child = Puzzle(self.matrix)
  62.         child.history = copy.deepcopy(self.history)
  63.         child.pos = copy.deepcopy(self.pos)
  64.        
  65.         return child
  66.        
  67.     def start(self, pos, direction1 = 0):
  68.         self.pos = pos
  69.        
  70.         if(self.matrix[self.pos[0]][self.pos[1]] == 1 and direction1 == 0):
  71.             return 0
  72.        
  73.         self.matrix[pos[0]][pos[1]] = 1
  74.        
  75.         while 1:
  76.             if direction1 == 0:
  77.                 possibleDirections = self.getPossibleDirections()
  78.             else:
  79.                 possibleDirections = [direction1]
  80.                 direction1 = 0
  81.             if len(possibleDirections) == 0:
  82.                 if self.isCompleted():
  83.                     self.solutions.append(self.history)
  84.                     return self.solutions
  85.                 else:
  86.                     if len(self.solutions) > 0:
  87.                         return self.solutions
  88.                     else:
  89.                         return 0
  90.        
  91.             directionCount = 0
  92.             child = self.branch()
  93.             for direction in possibleDirections:
  94.                 if directionCount == 0:
  95.                     self.go(direction)
  96.                 else:
  97.                     child1 = child.branch()
  98.                     r = child1.start(child1.pos,direction)
  99.                     if r != 0:
  100.                         self.solutions = self.solutions + child1.solutions
  101.                 directionCount += 1
  102.    
  103.     def findSolutions(matrix):
  104.         for line in range(len(matrix)):
  105.             for col in range(len(matrix[0])):
  106.                 p = Puzzle(matrix)
  107.                 if p.start([line,col]):
  108.                     print str(line) + " " + str(col) + " " + str(p.solutions)
  109.                
  110.     findSolutions = staticmethod(findSolutions)
  111.                
  112. if __name__ == "__main__":
  113.     matrix = [[0,0,1,1,0],
  114.             [0,0,0,0,0],
  115.             [0,0,0,0,0],
  116.             [0,0,0,0,0],
  117.             [1,1,1,0,0]]
  118.            
  119.     Puzzle.findSolutions(matrix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement