Facebook onsite question

Anonymous User
3761
May 31, 2021
May 31, 2021
Given a MxN grid, something like:
|.|x|.|.|.|.|
|.|x|.|.|.|.|
|.|x|x|x|.|.|
|.|B|.|.|.|.|
|.|.|.|.|.|.|
|.|S|.|.|.|E|

Here
S = start position of a person
E = end/target position of that person
B = ball
x = blocked cell
. = empty cell

Question - Can person starting at S push the ball at B to the end position E. It need not be the shortest path. But the person can push the ball only from either of 4 side (left,right,top,bottom).
Return True if person can push the ball to target else return False
In the above example, the person can push ball from left side and then once ball reaches right border, then can push from top till it reaches E

Can someone tell how to solve this ?
Let me know if you have any doubt about the question.

Update 1: question is similar to https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location/
Thanks to @sagarwal007 for pointing it out.

Update 2: Solution for this LC1263 -

class Solution:
    def minPushBox(self, grid: List[List[str]]) -> int:
        if not grid:
            return -1
        m, n = len(grid), len(grid[0])
        # get coordinates of player, box and target
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 'S':
                    player = (i, j)
                elif grid[i][j] == 'B':
                    box = (i, j)
                elif grid[i][j] == 'T':
                    target = (i, j)
                    
        direc = [(1,0), (-1,0), (0,1), (0,-1)]
        
        # check if coordinates are in grid and not equal to wall:
        def isValid(i, j):
            return 0 <= i < m and 0 <= j < n and grid[i][j] != '#'
        
        # check if player can reach to destination coordinates:
        def check(p, bx, dest):
            # print('check', p, bx, dest)
            qe = collections.deque()
            qe.append(p)
            v = set()
            while qe:
                i, j = qe.popleft()
                if (i,j) == dest:
                    return True
                for di, dj in direc:
                    ni, nj = i+di, j+dj
                    if isValid(ni, nj) and (ni,nj) not in v and (ni,nj) != bx:
                        v.add((ni, nj))
                        qe.append((ni, nj))
            return False
        
        # BFS from box to target - interanally we'll run another BFS
        q = collections.deque()
        q.append([0, box, player])
        visited = set()
        visited.add(box+player)
        while q:
            dist, b, p = q.popleft()
            # print(dist, b, p)
            if b == target:
                return dist
            boxX, boxY = b
            playerX, playerY = p
            for dx, dy in direc:
                nBoxX, nBoxY = boxX+dx, boxY+dy
                nPlayerX, nPlayerY = boxX-dx, boxY-dy # corresponding position of player in order to move the box
                if isValid(nBoxX, nBoxY) and (boxX, boxY, nBoxX, nBoxY) not in visited:
                    # print(nPlayerX, nPlayerY, boxX, boxY, nBoxX, nBoxY)
                    if isValid(nPlayerX, nPlayerY) and check(player, b, (nPlayerX, nPlayerY)):
                        # print('here')
                        visited.add((boxX, boxY, nBoxX, nBoxY))
                        q.append([dist+1, (nBoxX, nBoxY), b])
        return -1
		```
Comments (7)