- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Distance from a given Cell to all other Cells of a Matrix
In this problem, we need to find the distance of each cell from the given cell of the matrix.
We will use the Breadth−first search traversal to visit each cell of the matrix from the given cell and find the minimum distance for each cell.
Problem statement − We have given rows, cols, a, and b positive integers. Here, rows and cols represent the matrix's total number of rows and columns. The a and b is the cell of the matrix. We need to find the minimum distance of each cell of the matrix from the (a, b) cell.
Sample examples
Input
rows = 6, cols = 6, a = 3, b = 3
Output
3 3 3 3 3 3 3 2 2 2 2 2 3 2 1 1 1 2 3 2 1 0 1 2 3 2 1 1 1 2 3 2 2 2 2 2
Explanation − We have printed the distance of each cell from the (3, 3) cell.
Input
rows = 2, cols = 2, a = 0, b = 0;
Output
0 1 1 1
Explanation − The distance from each cell is 1 from the (0, 0) cell.
Approach
Here, we will use the queue to store each visited cell. After that, we will pop each pair from the queue and traverse in all 8 directions to find the distance of the other nearest cell based on the distance of the current cell from the (a, b) node.
Algorithm
Step 1 − Define the t1 and t2 variables and initialize them with the a and b variables.
Step 2 − Define the direX[] and direY[] array to store each possible direction from the current cell.
Step 3 − Define the queue to store the pair of matrix cells.
Step 4 − Insert the {a, b} pair into the queue, and initialize the matrix[a][b] with 0.
Step 5 − Traverse the queue
Step 5.1 − Pop the first pair from the queue in the loop.
Step 5.2 − Traverse the direX[] and direY[] array to move in all 8 directions.
Step 5.2.1 − Store the a + direX[p] into the temp_x and b + direY[p] into the temp_y variables.
Step 5.2.2 − If temp_x, temp_y is less than 0 or temp_x is greater than cols, or temp_y is greater than rows, move to the next iteration.
Step 5.2.3 − If matrix[temp_x][temp_y] is 0, update it with the matrix[a][b] + 1. Also, insert the updated pair into the queue.
Step 6 − Update the matrix[t1][t2] with 0.
Step 7 − Print matrix values.
Example
#include <bits/stdc++.h> using namespace std; int matrix[1001][1001]; void getMinDistance(int rows, int cols, int a, int b) { int t1 = a, t2 = b; // All directions int direX[] = {0, -1, -1, -1, 0, 1, 1, 1}; int direY[] = {1, 1, 0, -1, -1, -1, 0, 1}; queue<pair<int, int>> que; // Push the first pair to the queue que.push({a, b}); matrix[a][b] = 0; while (!que.empty()) { // Get pair from top of the queue a = que.front().first; b = que.front().second; // Pop them que.pop(); for (int p = 0; p < 8; p++) { int temp_x = a + direX[p]; int temp_y = b + direY[p]; // Boundary index validation if (temp_x < 0 || temp_x >= rows || temp_y >= cols || temp_y < 0) continue; // For non-visited cells if (matrix[temp_x][temp_y] == 0) { // Update minimum distance matrix[temp_x][temp_y] = matrix[a][b] + 1; // Insert new pair to queue que.push({temp_x, temp_y}); } } } matrix[t1][t2] = 0; for (int p = 0; p < rows; p++) { for (int q = 0; q < cols; q++) { cout << matrix[p][q] << " "; } cout << endl; } } int main() { int rows = 6, cols = 6, a = 3, b = 3; getMinDistance(rows, cols, a, b); }
Output
3 3 3 3 3 3 3 2 2 2 2 2 3 2 1 1 1 2 3 2 1 0 1 2 3 2 1 1 1 2 3 2 2 2 2 2
Time complexity − O(rows*cols) to create a matrix.
Space complexity − O(rows * cols)
Here, we use the BFS algorithm with the matrix to find the minimum distance of each cell from the source cell. In the BFS, when we visit the adjacent node of each node one by one. So, it always gives the minimum distance when we reach the particular cell for the first time.
To Continue Learning Please Login
Login with Google