zero_pos = self.current_position(0, 0) if zero_pos[0] == 1 and zero_pos[1] > 1: total_moves += self.solve_row1_tile(zero_pos[1]) zero_pos = self.current_position(0, 0) if zero_pos[0] == 0 and zero_pos[1] > 1: total_moves += self.solve_row0_tile(zero_pos[1]) zero_pos = self.current_position(0, 0) if zero_pos == (1, 1): total_moves += self.solve_2x2() zero_pos = self.current_position(0, 0) return total_moves # Start interactive simulation poc_fifteen_gui.FifteenGUI(Puzzle(4, 4)) #obj = Puzzle(3, 3, [[0, 1, 2], [3, 4, 5], [6, 7, 8]]) #print "Expecting True, received ", obj.row0_invariant(0) #obj = Puzzle(3, 3, [[4, 3, 2], [1, 0, 5], [6, 7, 8]]) #print "Expecting True, received ", obj.row1_invariant(1) #obj = Puzzle(3, 3, [[4, 3, 2], [1, 0, 5], [6, 7, 8]])#, obj.solve_2x2() returned incorrect move string '' #print obj.solve_2x2() #obj = Puzzle(3, 3, [[3, 2, 1], [6, 5, 4], [0, 7, 8]])#, obj.solve_col0_tile(2) returned incorrect move string '' #print obj.solve_col0_tile(2) #obj = Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]])#, obj.solve_interior_tile(2, 2) returned incorrect move string (Exception: AssertionError) "" at line 157, in solve_interior_tile #print obj.solve_interior_tile(2, 2) #obj = Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]])#, obj.solve_puzzle() returned incorrect move string '' #print obj.solve_puzzle() #obj = Puzzle(3, 3, [[4, 1, 0], [2, 3, 5], [6, 7, 8]])#, obj.solve_row0_tile(2) returned incorrect move string '' #print obj.solve_row0_tile(2)
# replace with your code return "" # Start interactive simulation # initial_grid = [[1, 3, 4], [2, 7, 5], [6, 8, 0], [9, 10, 11]] # initial_grid = [[8, 1, 2, 3], [4, 7, 5, 10], [9, 6, 0, 11], [12, 13, 14, 15]] # initial_grid = [[4, 13, 1, 3], [5, 10, 2, 7], [8, 12, 6, 11], [9, 0, 14, 15]] # initial_grid = [[4, 8, 7, 2], [10, 1, 5, 3], [9, 6, 0, 11], [12, 13, 14, 15]] # initial_grid = [[7, 5, 1, 9], [2, 4, 3, 8], [0, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[1, 2], [0, 4], [3, 5]] # initial_grid = [[1, 2],[0, 4],[3, 5]] # initial_grid = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]] # rdlurldrlrurrldrullldrrulrdruldluldrurdruldr: # initial_grid = [[1, 6, 5, 4], [3, 7, 2, 0], [8, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[1, 7, 6, 4], [3, 5, 2, 0], [8, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[1, 6, 2, 4], [3, 5, 0, 7], [8, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[4, 2, 1], [3, 0, 5]] # initial_grid = [[3, 5, 4, 0], [2, 1, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[0, 2], [3, 1]] # initial_grid = [[4, 2, 1], [3, 5, 0]] # initial_grid = [[3, 5, 4, 0], [2, 1, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[4, 1, 7], [2, 6, 5], [3, 0, 8]] # initial_grid = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] # initial_grid = [[1, 6, 5, 4], [3, 7, 2, 0], [8, 9, 10, 11], [12, 13, 14, 15]] # initial_grid = [[1, 6, 5, 4], [3, 7, 2, 0], [8, 9, 10, 11], [12, 13, 14, 15]] initial_grid = [[8, 2, 11, 7], [6, 0, 4, 3], [9, 5, 1, 10]] # initial_grid = [[4, 2, 10, 7, 9], [1, 0, 11, 8, 3], [17, 6, 18, 14, 12], [5, 19, 15, 16, 13]] poc_fifteen_gui.FifteenGUI(Puzzle(3, 4, initial_grid))
if col == 0: string += self.solve_col0_tile(row) else: string += self.solve_interior_tile(row, col) for col in range(self._width - 1, 1, -1): print "fix positon:", col string += self.solve_row1_tile(col) print "fixed row 1", self string += self.solve_row0_tile(col) print "fixed row 0", self string += self.solve_2x2() print "fixed row 2x2", self #self.update_puzzle(string) return string0 + string #testpuzzle = Puzzle(3, 2, [[1, 4], [2,3], [0,5]]) #testpuzzle = Puzzle(4, 5, [[15, 16, 0, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [1, 2, 17, 18, 19]]) #print testpuzzle.solve_puzzle() # Start interactive simulation poc_fifteen_gui.FifteenGUI(Puzzle(2, 2))
Updates the puzzle and returns a move string """ height=self.get_height() width=self.get_width() move_str='' current_row,current_col=self.current_position(0,0) row_diff = current_row - (height-1) col_diff = current_col - (width-1) move = abs(row_diff) * 'd' + abs(col_diff) * 'r' move = self.move_str_trimmer(move) self.update_puzzle(move) move_str += move for row in range(height-1,1,-1): for col in range(width-1,0,-1): move_str+=self.solve_interior_tile(row,col) move_str+=self.solve_col0_tile(row) for col in range(width-1,1,-1): move_str += self.solve_row1_tile(col) move_str += self.solve_row0_tile(col) move_str += self.solve_2x2() return move_str poc_fifteen_gui.FifteenGUI(Puzzle(4, 4, [[15, 11, 8, 12], [14, 10, 9, 13], [2, 6, 1, 4], [3, 7, 5, 0]])) My_Puzzle = Puzzle(4, 4, [[15, 11, 8, 12], [14, 10, 9, 13], [2, 6, 1, 4], [3, 7, 5, 0]]) print My_Puzzle.solve_puzzle()
def test(): """ To Test multiple test scenarios """ puzzle = Puzzle(4, 4) poc_fifteen_gui.FifteenGUI(puzzle)
tile0 = self.get_pos(0) if (tile0[0] > 1 and tile0[1] > 0): mk_str += self.solve_interior_tile(tile0[0], tile0[1]) elif tile0[0] > 1 and tile0[1] == 0: mk_str += self.solve_col0_tile(tile0[0]) elif tile0[0] == 1 and tile0[1] > 1: mk_str += self.solve_row1_tile(tile0[1]) elif tile0[0] == 0 and tile0[1] > 1: mk_str += self.solve_row0_tile(tile0[1]) elif (tile0[0] == 1 or tile0[0] == 0) and (tile0[1] == 1 or tile0[1] == 0): mk_str += self.solve_2x2() tgt_tile = self.get_pos(0)[0] * width + self.get_pos(0)[1] return mk_str # Start interactive simulation GRID = Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]]) #GRID = Puzzle(3, 6, [[16, 7, 13, 17, 5, 9], [3, 0, 14, 10, 12, 6], [4, 15, 2, 11, 8, 1]]) #GRID = Puzzle(4, 5, [[15, 16, 0, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [1, 2, 17, 18, 19]]) #GRID = Puzzle(4, 5, [[7, 6, 5, 3, 0], [4, 8, 2, 1, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) #GRID = Puzzle(3, 3, [[4, 1, 0], [2, 3, 5], [6, 7, 8]]) GRID.solve_puzzle() poc_fifteen_gui.FifteenGUI(GRID)
#my_puzzle.solve_interior_tile(2,1) #print my_puzzle #my_puzzle.solve_interior_tile(2,0) #print my_puzzle #my_puzzle = Puzzle(4, 4, [[4, 13, 1, 3], [5, 10, 2, 7], [8, 12, 6, 11], [9, 14, 0, 15]]) #my_puzzle = Puzzle(3, 3, [[0, 1, 2], [3, 4, 5], [6, 7, 8]]) #my_puzzle = Puzzle(3, 6, [[16, 7, 13, 17, 5, 9], [3, 0, 14, 10, 12, 6], [4, 15, 2, 11, 8, 1]]) #print my_puzzle #print my_puzzle.solve_puzzle() #print my_puzzle #poc_fifteen_gui.FifteenGUI(Puzzle(3, 6, [[16, 7, 13, 17, 5, 9], [3, 0, 14, 10, 12, 6], [4, 15, 2, 11, 8, 1]])) #my_puzzle = Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]]) #print my_puzzle #my_puzzle.solve_puzzle() #print my_puzzle #my_puzzle.solve_2x2() #my_puzzle.solve_col0_tile(3) #print my_puzzle #my_puzzle.solve_interior_tile(3, 0) #print my_puzzle #my_puzzle.solve_interior_tile(2, 2) #print my_puzzle my_puzzle = Puzzle(6, 6, [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]) poc_fifteen_gui.FifteenGUI( Puzzle(6, 6, [[1, 2, 3, 4, 5, 6], [7, 8, 0, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 9]]))
# move zero to the untarget position while self.get_number(tile[0],tile[1]) != 0: zero = self.current_position(0, 0) move_string_target = self.move_zero_to_pos(zero,tile) self.update_puzzle(move_string_target) move_string += move_string_target zero = self.current_position(0, 0) while called_time <20 and not self.validate_puzzle_solved(self._height,self._width)[0]: if zero[0] > 1 and zero[1] > 0: move_string += self.solve_interior_tile(zero[0], zero[1]) zero = self.current_position(0, 0) elif zero[0] > 1 and zero[1] == 0: move_string += self.solve_col0_tile(zero[0]) zero = self.current_position(0, 0) elif zero[0] == 1 and zero[1] >= 2: move_string += self.solve_row1_tile(zero[1]) zero = self.current_position(0, 0) elif zero[0] == 0 and zero[1] >= 0: move_string += self.solve_row0_tile(zero[1]) zero = self.current_position(0, 0) elif (zero[0] == 0 or zero[0] == 1) and (zero[1] == 0 or zero[1] == 1): move_string += self.solve_2x2() zero = self.current_position(0, 0) return move_string #Start interactive simulation #poc_fifteen_gui.FifteenGUI(Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]])) poc_fifteen_gui.FifteenGUI(Puzzle(5, 5, [[24, 23, 22,21,20], [19, 18, 17,16,15], [14, 13, 12,11,10],[9,8,7,6,5],[4,3,2,1,0]])) #poc_fifteen_gui.FifteenGUI(Puzzle(4, 4, [[15, 14, 13, 12], [11,10,9,8],[7, 6,5,4], [3,2, 1, 0]])) #poc_fifteen_gui.FifteenGUI(Puzzle(4, 5, [[12, 11, 9, 3, 4], [7, 11, 5, 14, 13], [1, 6, 8, 15, 0], [2, 16, 17, 18, 19]]))
move_string += self.solve_col0_tile(start_pos[0]) # self.update_puzzle(self.solve_col0_tile(start_pos[0])) start_pos[0] -= 1 start_pos[1] = self._width - 1 elif start_pos[0] <= 1: if start_pos[1] >= 2: if start_pos[0] == 1: move_string += self.solve_row1_tile(start_pos[1]) # self.update_puzzle(self.solve_row1_tile(start_pos[1])) start_pos[0] = 0 elif start_pos[0] == 0: move_string += self.solve_row0_tile(start_pos[1]) # self.update_puzzle(self.solve_row0_tile(start_pos[1])) start_pos[0] = 1 start_pos[1] -= 1 elif start_pos[1] <= 1: move_string += self.solve_2x2() # self.update_puzzle(self.solve_2x2()) start_pos = [0, 0] # move_string = self.solve_interior_tile(start_row, start_col) # move_string = self.solve_col0_tile(start_row) # move_string = self.solve_row1_tile(start_col) # move_string = self.solve_row0_tile(start_col) # move_string = self.solve_2x2() return move_string # Start interactive simulation poc_fifteen_gui.FifteenGUI(Puzzle(5, 5))
results += puz_clone.solve_row1_tile(tmp_col) #print puz_clone #print "row0" results += puz_clone.solve_row0_tile(tmp_col) #print puz_clone results += puz_clone.solve_2x2() #print puz_clone #print "done" self.update_puzzle(results) return results # Start interactive simulation poc_fifteen_gui.FifteenGUI(Puzzle(3, 3, [[3, 2, 6], [4, 0, 5], [1, 7, 8]])) #poc_fifteen_gui.FifteenGUI(Puzzle(3, 3)) #poc_fifteen_gui.FifteenGUI(Puzzle(2, 2, [[0, 3], [1, 2]])) # Pass test_1 = Puzzle(3, 3, [[3, 2, 1], [4, 0, 5], [6, 7, 8]]) #print test_1.lower_row_invariant(1, 1) # Pass test_2 = Puzzle(3, 3, [[3, 2, 1], [5, 7, 4], [6, 0, 8]]) #print test_2.lower_row_invariant(2, 1) # test solve_interior_tile #test_3 = Puzzle(3, 3, [[7, 3, 1], [5, 2, 4], [6, 0, 8]]) #print test_3.solve_interior_tile(2, 1) #test_4 = Puzzle(3, 3, [[8, 3, 1], [5, 2, 4], [6, 7, 0]]) #print test_4.solve_interior_tile(2, 2)
moves_str += self.solve_row0_tile(col) assert self.row1_invariant(col - 1) # Solve the upper left 2×2 portion of the puzzle directly. assert self.row1_invariant(1) moves_str += self.solve_2x2() elif self._height <= 2 and self._width <= 2: assert self.row1_invariant(1) moves_str += self.solve_2x2() #elif self._height > 2 and self._width <= 2: print moves_str print self._grid return moves_str # Start interactive simulation poc_fifteen_gui.FifteenGUI(Puzzle(4, 4,[[2,1,0,9],[5,8,4,3],[6,7,10,11],[12,13,14,15]])) #print Puzzle(4, 4,[[2,1,0,9],[5,8,4,3],[6,7,10,11],[12,13,14,15]]) #Obj = Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]]) #print Obj #assert Obj.lower_row_invariant(2,2) #Obj.solve_interior_tile(2, 2) #assert Obj.lower_row_invariant(2,2-1) # #Obj = Puzzle(3, 2, [[2,4], [3,1], [0,5]]) #print Obj #assert Obj.lower_row_invariant(2, 0) #Obj.solve_col0_tile(2) #assert Obj.lower_row_invariant(2 - 1, 2 - 1) # #obj = Puzzle(3, 3, [[3, 2, 1], [6, 5, 4], [0, 7, 8]]) #print obj
return self._grid[0][0] == 0 and self._grid[0][1] == 1 \ and self._grid[1][0] == self._width*1 and self._grid[1][1] == (1 + self._width * 1) def solve_puzzle(self): """ Generate a solution string for a puzzle Updates the puzzle and returns a move string """ cur0_row, cur0_col = self.current_position(0, 0) move_str = 'd' * (self._height - cur0_row - 1) + 'r' * (self._width - cur0_col - 1) self.update_puzzle(move_str) for row in range(self._height - 1, 1, -1): for col in range(self._width - 1, -1, -1): assert self.lower_row_invariant(row, col) if col != 0: move_str += self.solve_interior_tile(row, col) else: move_str += self.solve_col0_tile(row) for col in range(self._width - 1, 1, -1): assert self.row1_invariant(col) move_str += self.solve_row1_tile(col) assert self.row0_invariant(col) move_str += self.solve_row0_tile(col) move_str += self.solve_2x2() return move_str # Start interactive simulation poc_fifteen_gui.FifteenGUI(Puzzle(3, 3, [[8, 7, 6], [5, 4, 3], [2, 1, 0]]))
for dummy_num in range(delta_zero_y): thread = "d" my_string += thread prime_copy.update_puzzle(thread) print "first", "\n", prime_copy for target_row in range(prime_copy.get_height() - 1, 1, -1): for target_col in range(prime_copy.get_width() - 1, 0, -1): my_string += prime_copy.solve_interior_tile( target_row, target_col) my_string += prime_copy.solve_col0_tile(target_row) #self.update_puzzle(my_string) for target_col in range(prime_copy.get_width() - 1, 1, -1): my_string += prime_copy.solve_row1_tile(target_col) my_string += prime_copy.solve_row0_tile(target_col) my_string += prime_copy.solve_2x2() print self self.update_puzzle(my_string) return my_string # Start interactive simulation obj = Puzzle( 3, 6, [[16, 7, 13, 17, 5, 9], [3, 0, 14, 10, 12, 6], [4, 15, 2, 11, 8, 1]]) poc_fifteen_gui.FifteenGUI(obj) #obj.solve_puzzle()