Minimum distance to the end of a grid from source
Given a binary grid of order r * c and an initial position. The task is to find the minimum distance from the source to get to the end of the grid (first row, last row, first column or last column). A move can be made to a cell grid[i][j] only if grid[i][j] = 0 and only left, right, up and down movements are permitted. If no valid path exists then print -1.
Examples:
Input: i = 1, j = 1, grid[][] = { {1, 0, 1}, {0, 0, 0}, {1, 1, 1}}
Output: 1Input: i = 0, j = 0, grid[][] = { {0, 1}, {1, 1}}
Output: 0
Approach:
- If source is already the first/last row/column then print 0.
- Start traversing the grid starting with source using BFS as :
- Insert cell position in queue.
- Pop element from queue and mark it visited.
- For each valid move adjacent to popped one, insert the cell position into queue.
- On each move, update the minimum distance of the cell from initial position.
- After the completion of the BFS, find the minimum distance from source to every cell in the first row, last row, first column and last column.
- Print the minimum among these in the end.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define row 5 #define col 5 // Global variables for grid, minDistance and visited array int minDistance[row + 1][col + 1], visited[row + 1][col + 1]; // Queue for BFS queue<pair<int, int> > que; // Function to find whether the move is valid or not bool isValid(int grid[][col], int i, int j) { if (i < 0 || j < 0 || j >= col || i >= row || grid[i][j] || visited[i][j]) return false; return true; } // Function to return the minimum distance // from source to the end of the grid int findMinPathminDistance(int grid[][col], int sourceRow, int sourceCol) { // If source is one of the destinations if (sourceCol == 0 || sourceCol == col - 1 || sourceRow == 0 || sourceRow == row - 1) return 0; // Set minimum value int minFromSource = row * col; // Precalculate minDistance of each grid with R * C for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) minDistance[i][j] = row * col; // Insert source position in queue que.push(make_pair(sourceRow, sourceCol)); // Update minimum distance to visit source minDistance[sourceRow][sourceCol] = 0; // Set source to visited visited[sourceRow][sourceCol] = 1; // BFS approach for calculating the minDistance // of each cell from source while (!que.empty()) { // Iterate over all four cells adjacent // to current cell pair<int, int> cell = que.front(); // Initialize position of current cell int cellRow = cell.first; int cellCol = cell.second; // Cell below the current cell if (isValid(grid, cellRow + 1, cellCol)) { // Push new cell to the queue que.push(make_pair(cellRow + 1, cellCol)); // Update one of its neightbor's distance minDistance[cellRow + 1][cellCol] = min(minDistance[cellRow + 1][cellCol], minDistance[cellRow][cellCol] + 1); visited[cellRow + 1][cellCol] = 1; } // Above the current cell if (isValid(grid, cellRow - 1, cellCol)) { que.push(make_pair(cellRow - 1, cellCol)); minDistance[cellRow - 1][cellCol] = min(minDistance[cellRow - 1][cellCol], minDistance[cellRow][cellCol] + 1); visited[cellRow - 1][cellCol] = 1; } // Right cell if (isValid(grid, cellRow, cellCol + 1)) { que.push(make_pair(cellRow, cellCol + 1)); minDistance[cellRow][cellCol + 1] = min(minDistance[cellRow][cellCol + 1], minDistance[cellRow][cellCol] + 1); visited[cellRow][cellCol + 1] = 1; } // Left cell if (isValid(grid, cellRow, cellCol - 1)) { que.push(make_pair(cellRow, cellCol - 1)); minDistance[cellRow][cellCol - 1] = min(minDistance[cellRow][cellCol - 1], minDistance[cellRow][cellCol] + 1); visited[cellRow][cellCol - 1] = 1; } // Pop the the visited cell que.pop(); } int i; // Minimum distance in the first row for (i = 0; i < col; i++) minFromSource = min(minFromSource, minDistance[0][i]); // Minimum distance in the last row for (i = 0; i < col; i++) minFromSource = min(minFromSource, minDistance[row - 1][i]); // Minimum distance in the first column for (i = 0; i < row; i++) minFromSource = min(minFromSource, minDistance[i][0]); // Minimum distance in the last column for (i = 0; i < row; i++) minFromSource = min(minFromSource, minDistance[i][col - 1]); // If no path exists if (minFromSource == row * col) return -1; // Return the minimum distance return minFromSource; } // Driver code int main() { int sourceRow = 3, sourceCol = 3; int grid[row][col] = { 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0 }; cout << findMinPathminDistance(grid, sourceRow, sourceCol); return 0; } |
2
Recommended Posts:
- Minimum distance to the corner of a grid from source
- Shortest distance between two cells in a matrix or grid
- Minimum sum falling path in a NxN grid
- Minimum product in a grid of adjacent elements
- Minimum cost to cover the given positions in a N*M grid
- Count all possible walks from a source to a destination with exactly k edges
- Source to destination in 2-D path with fixed sized jumps
- Multi Source Shortest Path in Unweighted Graph
- Number of paths from source to destination in a directed acyclic graph
- Search a Word in a 2D Grid of characters
- Count possible moves in the given direction in a grid
- Largest connected component on a grid
- Check if a word exists in a grid or not
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Unique paths in a Grid with Obstacles
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



