Advertisement
Guest User

Minimum Moves to Move a Box to Their Target Location

a guest
Nov 19th, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. from collections import deque
  2.  
  3. class Solution:
  4.    
  5.     def is_valid(self, ri, ci, rn, cn):
  6.         if ri < 0 or ri >= rn or ci < 0 or ci >= cn:
  7.             return False
  8.        
  9.         return True
  10.    
  11.     def is_reachable(self, start_ri, start_ci, target_ri, target_ci, box_ri, box_ci, grid):
  12.        
  13.         rn = len(grid)
  14.         cn = len(grid[0])
  15.        
  16.         if not self.is_valid(target_ri, target_ci, rn, cn) or grid[target_ri][target_ci] == '#':
  17.             return False
  18.        
  19.         q = deque()
  20.         q.append((start_ri, start_ci))
  21.         visited = set()
  22.         visited.add((start_ri, start_ci))
  23.        
  24.         moves = [(-1, 0), (0, 1), (1, 0), (0, -1)]
  25.        
  26.         while q:
  27.            
  28.             ri, ci = q.popleft()
  29.            
  30.             if ri == target_ri and ci == target_ci:
  31.                 return True
  32.            
  33.             for kr, kc in moves:
  34.                
  35.                 xri = ri + kr
  36.                 xci = ci + kc
  37.                
  38.                 if self.is_valid(xri, xci, rn, cn) and \
  39.                    grid[xri][xci] != '#' and \
  40.                    (xri != box_ri or xci != box_ci) and \
  41.                    (xri, xci) not in visited:
  42.                     q.append((xri, xci))
  43.                     visited.add((xri, xci))              
  44.        
  45.         return False
  46.    
  47.     def minPushBox(self, grid: List[List[str]]) -> int:
  48.        
  49.         rn = len(grid)
  50.         cn = len(grid[0])
  51.        
  52.         for ri in range(rn):
  53.             for ci in range(cn):
  54.                 if grid[ri][ci] == 'T':
  55.                     t_ri, t_ci = ri, ci
  56.                 elif grid[ri][ci] == 'B':
  57.                     b_ri, b_ci = ri, ci
  58.                 elif grid[ri][ci] == 'S':
  59.                     s_ri, s_ci = ri, ci
  60.        
  61.         q = deque()
  62.         q.append((b_ri, b_ci, s_ri, s_ci, 0))
  63.         visited = set()
  64.         visited.add((b_ri, b_ci, s_ri, s_ci))
  65.        
  66.         moves = [(-1, 0), (0, 1), (1, 0), (0, -1)]
  67.        
  68.         while q:
  69.            
  70.             b_ri, b_ci, s_ri, s_ci, lvl = q.popleft()
  71.            
  72.             if b_ri == t_ri and b_ci == t_ci:
  73.                 return lvl
  74.            
  75.             for kr, kc in moves:
  76.                
  77.                 next_b_ri = b_ri + kr
  78.                 next_b_ci = b_ci + kc
  79.                
  80.                 s_push_ri = b_ri + -1 * (kr)
  81.                 s_push_ci = b_ci + -1 * (kc)
  82.                
  83.                 if self.is_valid(next_b_ri, next_b_ci, rn, cn) and \
  84.                    grid[next_b_ri][next_b_ci] != '#' and \
  85.                    self.is_reachable(s_ri, s_ci, s_push_ri, s_push_ci, b_ri, b_ci, grid) and \
  86.                    (next_b_ri, next_b_ci, s_push_ri, s_push_ci) not in visited:
  87.                     q.append((next_b_ri, next_b_ci, s_push_ri, s_push_ci, lvl+1))
  88.                     visited.add((next_b_ri, next_b_ci, s_push_ri, s_push_ci))
  89.        
  90.         return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement