Similar Problems
Similar Problems not available
Minimum Number Of Visited Cells In A Grid - Leetcode Solution
Companies:
LeetCode: Minimum Number Of Visited Cells In A Grid Leetcode Solution
Difficulty: Hard
Topics: dynamic-programming binary-search union-find stack array
Problem statement:
You are given a grid with m rows and n columns. You need to implement a function minVisitedCells that takes this grid as input and returns the minimum number of visited cells required to travel from the top-left cell (0,0) to the bottom-right cell (m-1,n-1), subject to the following constraints:
You can only move to a neighboring cell that is either to the right, down, or diagonally down-right. You cannot visit the same cell more than once. The input grid will contain only 0’s and 1’s. A 0 indicates that a cell is empty and a 1 indicates that there is an obstacle in that cell. You cannot move through obstacles. If there is no valid path from the top-left cell to the bottom-right cell, your function should return -1.
Solution:
To solve this problem, we can use dynamic programming approach. Let's define a 2D array dp where dp[i][j] represents the minimum number of visited cells required to travel from cell (0,0) to cell (i,j). The value of dp[m-1][n-1] will give the final answer.
To calculate the value of dp[i][j], we need to consider the following cases:
If cell (i,j) contains an obstacle (i.e., grid[i][j] == 1), then dp[i][j] must be set to infinity as we cannot move through obstacles. If (i,j) is the top-left cell (i.e., i == 0 and j == 0), then dp[i][j] = 1 as we have already visited this cell. If (i,j) is in the first row (i.e., i == 0), then dp[i][j] can only be reached by moving from the left cell (i.e., dp[i][j-1]). Hence, dp[i][j] = dp[i][j-1] + 1 if grid[i][j] == 0, else dp[i][j] = infinity. If (i,j) is in the first column (i.e., j == 0), then dp[i][j] can only be reached by moving from the top cell (i.e., dp[i-1][j]). Hence, dp[i][j] = dp[i-1][j] + 1 if grid[i][j] == 0, else dp[i][j] = infinity. If (i,j) is not in the first row or column (i.e., i > 0 and j > 0), then dp[i][j] can be reached from three directions - left, top, and diagonal top-left. We need to choose the direction that requires minimum number of visited cells. Hence, dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1 if grid[i][j] == 0, else dp[i][j] = infinity.
The final answer is dp[m-1][n-1]. If dp[m-1][n-1] is infinity, then there is no valid path from the top-left cell to the bottom-right cell, so we return -1.
Code:
Here is the code for the above approach:
int minVisitedCells(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); vector<vector<int>> dp(m, vector<int>(n, 0));
// Initialize dp[0][0] dp[0][0] = (grid[0][0] == 1)? INT_MAX : 1;
// Initialize first row of dp for (int j = 1; j < n; j++) { dp[0][j] = (grid[0][j] == 1)? INT_MAX : dp[0][j-1] + 1; }
// Initialize first column of dp for (int i = 1; i < m; i++) { dp[i][0] = (grid[i][0] == 1)? INT_MAX : dp[i-1][0] + 1; }
// Fill remaining cells of dp for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (grid[i][j] == 1) { dp[i][j] = INT_MAX; } else { dp[i][j] = min(min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1; } } }
// Return answer return (dp[m-1][n-1] == INT_MAX)? -1 : dp[m-1][n-1]; }
Time Complexity: O(mn), where m and n are the dimensions of the grid.
Space Complexity: O(mn), as we are using a 2D array of size m x n.
Minimum Number Of Visited Cells In A Grid Solution Code
1