• + 0 comments

    Solution in Python

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'minimumMoves' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts following parameters:
    #  1. STRING_ARRAY grid
    #  2. INTEGER startX
    #  3. INTEGER startY
    #  4. INTEGER goalX
    #  5. INTEGER goalY
    #
    
    from collections import deque
    
    def minimumMoves(grid, startX, startY, goalX, goalY):
        # Write your code here
        n = len(grid)
        m = len(grid[0])
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]    
        queue = deque([(startX, startY, 0)])  
        visited = set()
        visited.add((startX, startY))
        while queue:
            x, y, moves = queue.popleft()        
            if (x, y) == (goalX, goalY):
                return moves        
            for dx, dy in directions:
                nx, ny = x, y
                while 0 <= nx + dx < n and 0 <= ny + dy < m and grid[nx + dx][ny + dy] != 'X':
                    nx += dx
                    ny += dy
                    if (nx, ny) not in visited:
                        visited.add((nx, ny))
                        queue.append((nx, ny, moves + 1))    
        return -1
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        n = int(input().strip())
    
        grid = []
    
        for _ in range(n):
            grid_item = input()
            grid.append(grid_item)
    
        first_multiple_input = input().rstrip().split()
    
        startX = int(first_multiple_input[0])
    
        startY = int(first_multiple_input[1])
    
        goalX = int(first_multiple_input[2])
    
        goalY = int(first_multiple_input[3])
    
        result = minimumMoves(grid, startX, startY, goalX, goalY)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()