Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1810.html

1810. Minimum Path Cost in a Hidden Grid

Level

Medium

Description

This is an interactive problem.

There is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid. The grid is of size m x n, and each cell in the grid is either empty or blocked. It is guaranteed that the starting cell and the target cell are different, and neither of them is blocked.

Each cell has a cost that you need to pay each time you move to the cell. The starting cell’s cost is not applied before the robot moves.

You want to find the minimum total cost to move the robot to the target cell. However, you do not know the grid’s dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the GridMaster object.

The GridMaster class has the following functions:

  • boolean canMove(char direction) Returns true if the robot can move in that direction. Otherwise, it returns false.
  • int move(char direction) Moves the robot in that direction and returns the cost of moving to that cell. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, the robot will remain in the same position, and the function will return -1.
  • boolean isTarget() Returns true if the robot is currently on the target cell. Otherwise, it returns false.

Note that direction in the above functions should be a character from {'U','D','L','R'}, representing the directions up, down, left, and right, respectively.

Return the minimum total cost to get the robot from its initial starting cell to the target cell. If there is no valid path between the cells, return -1.

Custom testing:

The test input is read as a 2D matrix grid of size m x n and four integers r1, c1, r2, and c2 where:

  • grid[i][j] == 0 indicates that the cell (i, j) is blocked.
  • grid[i][j] >= 1 indicates that the cell (i, j) is empty and grid[i][j] is the cost to move to that cell.
  • (r1, c1) is the starting cell of the robot.
  • (r2, c2) is the target cell of the robot.

Remember that you will not have this information in your code.

Example 1:

Input: grid = [[2,3],[1,1]], r1 = 0, c1 = 1, r2 = 1, c2 = 0

Output: 2

Explanation: One possible interaction is described below:

The robot is initially standing on cell (0, 1), denoted by the 3.

  • master.canMove(‘U’) returns false.
  • master.canMove(‘D’) returns true.
  • master.canMove(‘L’) returns true.
  • master.canMove(‘R’) returns false.
  • master.move(‘L’) moves the robot to the cell (0, 0) and returns 2.
  • master.isTarget() returns false.
  • master.canMove(‘U’) returns false.
  • master.canMove(‘D’) returns true.
  • master.canMove(‘L’) returns false.
  • master.canMove(‘R’) returns true.
  • master.move(‘D’) moves the robot to the cell (1, 0) and returns 1.
  • master.isTarget() returns true.
  • master.move(‘L’) doesn’t move the robot and returns -1.
  • master.move(‘R’) moves the robot to the cell (1, 1) and returns 1.

We now know that the target is the cell (0, 1), and the minimum total cost to reach it is 2.

Example 2:

Input: grid = [[0,3,1],[3,4,2],[1,2,0]], r1 = 2, c1 = 0, r2 = 0, c2 = 2

Output: 9

Explanation: The minimum cost path is (2,0) -> (2,1) -> (1,1) -> (1,2) -> (0,2).

Example 3:

Input: grid = [[1,0],[0,1]], r1 = 0, c1 = 0, r2 = 1, c2 = 1

Output: -1

Explanation: There is no path from the robot to the target cell.

Constraints:

  • 1 <= n, m <= 100
  • m == grid.length
  • n == grid[i].length
  • 0 <= grid[i][j] <= 100

Solution

First, do depth first starting from the original cell of the robot to figure out all values in grid and determine whether the target cell can be reached. It the target cell cannot be reached, return -1. Otherwise, use a priority queue to store the cells and the corresponding costs to be visited. Once the target cell is reached, return the minimum path cost.

  • /**
     * // This is the GridMaster's API interface.
     * // You should not implement it, or speculate about its implementation
     * class GridMaster {
     *     boolean canMove(char direction);
     *     int move(char direction);
     *     boolean isTarget();
     * }
     */
    
    class Solution {
        static final int MAX = 100;
        char[] directionsChar = {'U', 'L', 'D', 'R'};
        int[][] directionsArr = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
        boolean flag = false;
        int endRow = 0, endColumn = 0;
    
        public int findShortestPath(GridMaster master) {
            int[][] grid = new int[MAX * 2][MAX * 2];
            for (int i = 0; i < MAX * 2; i++)
                Arrays.fill(grid[i], -1);
            depthFirstSearch(MAX, MAX, grid, master);
            if (!flag)
                return -1;
            PriorityQueue<int[]> priorityQueue = new PriorityQueue<int[]>(new Comparator<int[]>() {
                public int compare(int[] cell1, int[] cell2) {
                    return cell1[2] - cell2[2];
                }
            });
            priorityQueue.offer(new int[]{100, 100, 0});
            while (!priorityQueue.isEmpty()) {
                int[] cell = priorityQueue.poll();
                int row = cell[0], column = cell[1], cost = cell[2];
                if (row == endRow && column == endColumn)
                    return cost;
                for (int[] directionArr : directionsArr) {
                    int newRow = row + directionArr[0], newColumn = column + directionArr[1];
                    if (newRow >= 0 && newRow < MAX * 2 && newColumn >= 0 && newColumn < MAX * 2 && grid[newRow][newColumn] > 0) {
                        int newCost = cost + grid[newRow][newColumn];
                        grid[newRow][newColumn] = -1;
                        priorityQueue.offer(new int[]{newRow, newColumn, newCost});
                    }
                }
            }
            return -1;
        }
    
        public void depthFirstSearch(int row, int column, int[][] grid, GridMaster master) {
            if (master.isTarget()) {
                endRow = row;
                endColumn = column;
                flag = true;
            }
            for (int i = 0; i < 4; i++) {
                char directionChar = directionsChar[i];
                char oppositeDirectionChar = directionsChar[(i + 2) % 4];
                int[] directionArr = directionsArr[i];
                int newRow = row + directionArr[0], newColumn = column + directionArr[1];
                if (master.canMove(directionChar) && grid[newRow][newColumn] < 0) {
                    grid[newRow][newColumn] = master.move(directionChar);
                    depthFirstSearch(newRow, newColumn, grid, master);
                    master.move(oppositeDirectionChar);
                }
            }
        }
    }
    
    ############
    
    /**
     * // This is the GridMaster's API interface.
     * // You should not implement it, or speculate about its implementation
     * class GridMaster {
     *     boolean canMove(char direction);
     *     int move(char direction);
     *     boolean isTarget();
     * }
     */
    
    class Solution {
        private static final char[] dir = {'U', 'R', 'D', 'L'};
        private static final char[] ndir = {'D', 'L', 'U', 'R'};
        private static final int[] dirs = {-1, 0, 1, 0, -1};
        private static final int N = 200;
        private static final int INF = 0x3f3f3f3f;
        private static int[][] g = new int[N][N];
        private static int[][] dist = new int[N][N];
        private int[] target;
    
        public int findShortestPath(GridMaster master) {
            target = new int[] {-1, -1};
            for (int i = 0; i < N; ++i) {
                Arrays.fill(g[i], -1);
                Arrays.fill(dist[i], INF);
            }
            dfs(100, 100, master);
            if (target[0] == -1 && target[1] == -1) {
                return -1;
            }
            PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
            q.offer(new int[] {0, 100, 100});
            dist[100][100] = 0;
            while (!q.isEmpty()) {
                int[] p = q.poll();
                int w = p[0], i = p[1], j = p[2];
                if (i == target[0] && j == target[1]) {
                    return w;
                }
                for (int k = 0; k < 4; ++k) {
                    int x = i + dirs[k], y = j + dirs[k + 1];
                    if (x >= 0 && x < N && y >= 0 && y < N && g[x][y] != -1
                        && dist[x][y] > w + g[x][y]) {
                        dist[x][y] = w + g[x][y];
                        q.offer(new int[] {dist[x][y], x, y});
                    }
                }
            }
            return 0;
        }
    
        private void dfs(int i, int j, GridMaster master) {
            if (master.isTarget()) {
                target = new int[] {i, j};
            }
            for (int k = 0; k < 4; ++k) {
                char d = dir[k], nd = ndir[k];
                int x = i + dirs[k], y = j + dirs[k + 1];
                if (x >= 0 && x < N && y >= 0 && y < N && master.canMove(d) && g[x][y] == -1) {
                    g[x][y] = master.move(d);
                    dfs(x, y, master);
                    master.move(nd);
                }
            }
        }
    }
    
  • // OJ: https://leetcode.com/problems/minimum-path-cost-in-a-hidden-grid/
    // Time: O(MNlog(MN))
    // Space: O(MN)
    const int maxN = 100, INF = 0x3f3f3f3f, init = maxN + 1;
    class Solution {
        int grid[2 * maxN + 2][2 * maxN + 2] = {}, dist[2 * maxN + 2][2 * maxN + 2] = {}, tx = INF, ty = INF;
        tuple<char, int, int> dirs[4] = { {'U',-1,0}, {'D',1,0},{'R',0,1},{'L',0,-1} };
        void dfs(GridMaster &m, int x = init, int y = init) {
            if (m.isTarget()) tx = x, ty = y;
            for (int i = 0; i < 4; ++i) {
                auto &[dir, dx, dy] = dirs[i];
                int a = x + dx, b = y + dy;
                if (grid[a][b] != INF || !m.canMove(dir)) continue;
                grid[a][b] = m.move(dir);
                dfs(m, a, b);
                m.move(get<0>(dirs[i % 2 ? i - 1 : i + 1]));
            }
        }
    public:
        int findShortestPath(GridMaster &m) {
            memset(grid, 0x3f, sizeof(grid));
            dfs(m);
            if (tx == INF) return -1;
            priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> pq;
            pq.push({0, init, init});
            memset(dist, 0x3f, sizeof(dist));
            dist[init][init] = 0;
            while (pq.size()) {
                auto [d, x, y] = pq.top();
                pq.pop();
                if (d > dist[x][y]) continue;
                if (x == tx && y == ty) return d;
                for (auto &[dir, dx, dy] : dirs) {
                    int a = x + dx, b = y + dy;
                    if (grid[a][b] == INF) continue;
                    int next = d + grid[a][b];
                    if (next < dist[a][b]) {
                        dist[a][b] = next;
                        pq.push({ next, a, b });
                    }
                }
            }
            return -1;
        }
    };
    
  • # """
    # This is GridMaster's API interface.
    # You should not implement it, or speculate about its implementation
    # """
    # class GridMaster(object):
    #    def canMove(self, direction: str) -> bool:
    #
    #
    #    def move(self, direction: str) -> int:
    #
    #
    #    def isTarget(self) -> None:
    #
    #
    
    
    class Solution(object):
        def findShortestPath(self, master: 'GridMaster') -> int:
            def dfs(i, j):
                nonlocal target
                if master.isTarget():
                    target = (i, j)
                for dir, (a, b, ndir) in dirs.items():
                    x, y = i + a, j + b
                    if 0 <= x < N and 0 <= y < N and master.canMove(dir) and g[x][y] == -1:
                        g[x][y] = master.move(dir)
                        dfs(x, y)
                        master.move(ndir)
    
            target = (-1, -1)
            N = 200
            INF = 0x3F3F3F3F
            g = [[-1] * N for _ in range(N)]
            dirs = {
                'U': (-1, 0, 'D'),
                'D': (1, 0, 'U'),
                'L': (0, -1, 'R'),
                'R': (0, 1, 'L'),
            }
            dfs(100, 100)
            if target == (-1, -1):
                return -1
            q = [(0, 100, 100)]
            dist = [[INF] * N for _ in range(N)]
            dist[100][100] = 0
            while q:
                w, i, j = heappop(q)
                if (i, j) == target:
                    return w
                for a, b, _ in dirs.values():
                    x, y = i + a, j + b
                    if (
                        0 <= x < N
                        and 0 <= y < N
                        and g[x][y] != -1
                        and dist[x][y] > w + g[x][y]
                    ):
                        dist[x][y] = w + g[x][y]
                        heappush(q, (dist[x][y], x, y))
            return 0
    
    
    

All Problems

All Solutions