Advertisement
bennyfromtheblock

1263. Minimum Moves to Move a Box to Their Target Location

Oct 14th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. # Lets break the question into simple parts:
  2.  
  3. # Lets think that we have no person and we have to find the minimum path between box and the target. Easy right? Simple BFS.
  4.  
  5. # If you know how to solve the first part, what I actually do is modify first part with few constraints.
  6.  
  7. # I just check whether the box can be shifted to the new position(up, down, left, right)
  8. # For it to be shifted to the new position the person has to be in a corresponding position right?
  9. # So we check if the person can travel from his old position to his corresponding new position(using another BFS).
  10. # If the person can travel to his new position than the box can be shifted, otherwise the box cannot be shifted.
  11. # We keep repeating step 2 until we reach the target or it is not possible to move the box anymore.
  12.  
  13. # TC: O(M*N * M*N) = O(M^2 N^2)
  14. # SC: O(M^2 N^2), since we are storing the state as box_pos + player_pos, there are M*N possibilities for each
  15.  
  16. class Solution:
  17.     def minPushBox(self, grid: List[List[str]]) -> int:
  18.         M = len(grid)
  19.         N = len(grid[0])
  20.        
  21.         for i in range(M):
  22.             for j in range(N):
  23.                 c = grid[i][j]
  24.                 if c == 'T':
  25.                     target = (i, j)
  26.                 elif c == 'B':
  27.                     box = (i, j)
  28.                 elif c == 'S':
  29.                     person = (i, j)
  30.                    
  31.        
  32.         # is this position an empty space in the grid?
  33.         def isSpace(pos):
  34.             row, col = pos[0], pos[1]
  35.             return row >= 0 and row < M and col >= 0 and col < N and grid[row][col] != '#'
  36.        
  37.         # Can the player go from its current position to the destination? (player can't walk on the box)
  38.         def playerCanVisit(playerPos, destination, box):
  39.             q = deque([playerPos])
  40.             visited = set()
  41.            
  42.             while q:
  43.                 pos = q.popleft()
  44.                 if pos == destination:
  45.                     return True
  46.                 new_positions = [(pos[0]+1, pos[1]), (pos[0]-1, pos[1]), (pos[0], pos[1]+1), (pos[0], pos[1]-1)]
  47.                 for new_pos in new_positions:
  48.                     if isSpace(new_pos) and new_pos != box and new_pos not in visited:
  49.                         q.append(new_pos)
  50.                         visited.add(new_pos)
  51.                        
  52.             return False
  53.        
  54.        
  55.         q = deque([(0, box, person)]) # distance travelled, box pos, player pos
  56.         visited = set([box + person]) # box coordinate concat player coordinate defines a visited state
  57.        
  58.         while q:
  59.             dist, box, player = q.popleft()
  60.             if box == target:
  61.                 return dist
  62.            
  63.             # potential new box cooridnates
  64.             box_coord = [(box[0]+1, box[1]), (box[0]-1, box[1]), (box[0], box[1]+1), (box[0], box[1]-1)]
  65.            
  66.             # positions player has to be in to push box to corresponding new coordinates
  67.             player_coord = [(box[0]-1, box[1]), (box[0]+1, box[1]), (box[0], box[1]-1), (box[0], box[1]+1)]
  68.            
  69.             for new_box, player_pos in zip(box_coord, player_coord):
  70.                 if isSpace(new_box) and new_box+box not in visited:
  71.                     if isSpace(player_pos) and playerCanVisit(player, player_pos, box):
  72.                         q.append((dist+1, new_box, box))
  73.                         visited.add(new_box + box)
  74.                        
  75.         return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement