
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check for possible path in 2D matrix in C++
Consider we have a 2D array. We have to find if we can get a path from topleft corner to bottom-right corner. The matrix is filled with 0s and 1s. 0 indicates open area, 1 indicates blockage. Note that the top-left corner will always be 1.
Suppose a matrix is like below −
0 | 0 | 0 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
0 | 0 | 0 | 1 | 0 |
1 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 0 |
One path is marked as green, there are some other paths also. So the program will return true, if there is a path, otherwise false.
We will solve this problem, by changing all accessible node to -1, First change the value of starting point to -1, then get next value in the first row, and compare to the previous value, set the current value equal to previous value iff it is reachable (not 0). Do the same for the column values also. By comparing and setting the current with the previous column values if that is reachable. Then starting from first row, first column, and take the values of previous row, previous column, get minimum between them. And set current index into the minimum. If the current index is 1, then there is no change. At the end if the final index is same as the bottom-right, then return true, otherwise false.
Example
#include <iostream> #define row 5 #define col 5 using namespace std; bool isPathPresent(int arr[row][col]) { arr[0][0] = -1; for (int i = 1; i < row; i++) if (arr[i][0] != 1) arr[i][0] = arr[i - 1][0]; for (int j = 1; j < col; j++) if (arr[0][j] != 1) arr[0][j] = arr[0][j - 1]; for (int i = 1; i < row; i++) for (int j = 1; j < col; j++) if (arr[i][j] != 1) arr[i][j] = min(arr[i][j - 1], arr[i - 1][j]); return (arr[row - 1][col - 1] == -1); } int main() { int arr[row][col] = {{ 0, 0, 0, 1, 0}, {1, 0, 0, 1, 1}, { 0, 0, 0, 1, 0}, {1, 0, 0, 0, 0}, { 0, 0, 1, 0, 0}}; if (isPathPresent(arr)) cout << "Path is present"; else cout << "No path has found"; }
Output
Path is present
- Related Articles
- Search a 2D Matrix in C++
- Maximum trace possible for any sub-matrix of the given matrix in C++
- Maximum path sum in matrix in C++
- Shortest Path in Binary Matrix in C++
- Print concentric rectangular pattern in a 2d matrix in C++
- Prefix Sum of Matrix (Or 2D Array) in C++
- Construct a linked list from 2D matrix in C++
- Maximum sum rectangle in a 2D matrix | DP-27 in C++
- Print 2D matrix in different lines and without curly braces in C/C++
- Maximum decimal value path in a binary matrix in C++
- Construct a linked list from 2D matrix (Iterative Approach) in C++
- Maximum sum rectangle in a 2D matrix
- Search a 2D Matrix II in Python
- Print a 2D Array or Matrix in Java
- Program to check diagonal matrix and scalar matrix in C++
