Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. ############################################################
  2. # Section 3: Linear Disk Movement
  3. ############################################################
  4.  
  5.  
  6. def solve_identical_disks(length, n):
  7.     graph = [[],[]] #a list of tuple moves, and a list of result boards
  8.     start_board=[]
  9.     for i in range(length):
  10.         if i<n:
  11.             start_board.append(1)
  12.         else:
  13.             start_board.append(0)
  14.     frontier = [[],start_board] # a list of tuple moves, and a list of result boards
  15.     tuple_board = list2tuple(start_board)
  16.     visited = set(tuple_board)
  17.     while frontier:
  18.         moves, current_board = frontier.pop()
  19.         if current_board.is_solved():
  20.             #also needs list of moves
  21.             return moves
  22.         else:
  23.             move, board = generate_possible_moves(current_board)    
  24.            
  25.            
  26.            
  27.             for move, new_p in current_board.successors():
  28.                    
  29.                     #print(new_p.board_list)
  30.                     new_p_tuple = list2tuple(new_p.board_list)
  31.                     #print(new_p_tuple)
  32.                     if new_p_tuple not in visited:
  33.                         temp_moves=moves+[move]
  34.                         #moves = moves + [move]
  35.                         #print(moves)
  36.                         frontier = [(temp_moves, new_p)] + frontier
  37.                         visited.add(new_p_tuple)
  38.        
  39.        
  40.    
  41.    
  42.    
  43.    
  44.     return start_board
  45.  
  46. def generate_possible_moves(board):
  47.     x = copy.deepcopy(board)
  48.     for i in range(len(board)):
  49.         if x[i]:
  50.             if not x[i+1]:
  51.                 yield (x[i],x[i+1]),move_piece(x,i,i+1)
  52.             if x[i+1] and not x[i+2]:
  53.                 yield (x[i],x[i+2]),move_piece(x,i,i+2)
  54.  
  55.        
  56.                
  57. def move_piece(board, start, end):
  58.     #make sure to check that a move is valid before sending it here
  59.     board[start]=0
  60.     board[end]=1
  61.     return board
  62.                
  63.    
  64.  
  65. def solve_distinct_disks(length, n):
  66.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement