# Exit Point in a Binary Matrix Using C++

A binary matrix refers to a grid with rows and columns composed exclusively of 0s or 1s within computer programming parlance. Identifying the exit point within a binary matrix constitutes a coding challenge encountered during programming interviews and competitions. In this write-up, we'll explain distinct approaches for addressing this problem with C++.

## Syntax

It may prove beneficial to first familiarize ourselves with the syntax that will feature prominently in our upcoming code examples before delving into the algorithm.

pair<int, int> findExitPoint(const vector<vector<int>>& matrix).


## Algorithm

Now, let's outline the step-by-step algorithm to find the exit point in a binary matrix −

• Initialize the current cell position as (0, 0).

• Start traversing the matrix from the current cell.

• On the off chance that the ongoing cell is 1, move to the following cell in priority order − right, down, left, up.

• In the event that the ongoing cell is 0, leave the loop and return the ongoing cell position as the exit point.

• Repeat steps 3 and 4 until an exit point is found or all cells are visited.

## Approach 1

In approach 1 method that we suggest for enacting the algorithm centers around implementing both a while loop and conditional statements. For reference, here’s an example of what this implementation would look like −

### Example

#include <iostream>
#include <vector>
using namespace std;

pair<int, int> findExitPoint(const vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix.size();
int x = 0, y = 0;  // Starting cell position

while (x >= 0 && x < rows && y >= 0 && y < cols) {
if (matrix[x][y] == 1) {
// Move right
if (y + 1 < cols && matrix[x][y + 1] == 1)
y++;
// Move down
else if (x + 1 < rows && matrix[x + 1][y] == 1)
x++;
// Move left
else if (y - 1 >= 0 && matrix[x][y - 1] == 1)
y--;
// Move up
else if (x - 1 >= 0 && matrix[x - 1][y] == 1)
x--;
} else {
break;  // Exit loop when encountering a 0
}
}

return make_pair(x, y);
}

int main() {
// Matrix initialization
vector<vector<int>> matrix = {
{1, 0, 0, 1},
{1, 1, 0, 1},
{0, 1, 1, 1},
{0, 0, 0, 1}
};

// Finding the exit point
pair<int, int> exitPoint = findExitPoint(matrix);

// Printing the exit point coordinates
cout << "Exit Point: (" << exitPoint.first << ", " << exitPoint.second << ")" << endl;

return 0;
}


### Output

Exit Point: (3, 3)


## Approach 2

To handle cell movement our second approach employs a do while loop in conjunction with switch statements. For reference, here’s an example of what this implementation would look like −

### Example

#include <iostream>
#include <vector>
using namespace std;

pair<int, int> findExitPoint(const vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix.size();
int x = 0, y = 0;  // Starting cell position

do {
switch (matrix[x][y]) {
case 1:  // Move based on the priority order
if (y + 1 < cols && matrix[x][y + 1] == 1) {
y++;  // Move right
} else if (x + 1 < rows && matrix[x + 1][y] == 1) {
x++;  // Move down
} else if (y - 1 >= 0 && matrix[x][y - 1] == 1) {
y--;  // Move left
} else if (x - 1 >= 0 && matrix[x - 1][y] == 1) {
x--;  // Move up
}
break;

default:  // Exit loop when encountering a 0
break;
}
} while (x >= 0 && x < rows && y >= 0 && y < cols);

return make_pair(x, y);
}

int main() {
// Matrix initialization
vector<vector<int>> matrix = {
{1, 0, 0, 1},
{1, 1, 0, 1},
{0, 1, 1, 1},
{0, 0, 0, 1}
};

// Finding the exit point
pair<int, int> exitPoint = findExitPoint(matrix);

// Printing the exit point coordinates
cout << "Exit Point: (" << exitPoint.first << ", " << exitPoint.second << ")" << endl;

return 0;
}


### Output

Exit Point: (3, 3)


### Explanation

The function findExitPoint was crafted in the provided code. Its purpose is to take a binary matrix as input and output a pair of integers which correspond to the exit point coordinates. The function follows the outlined algorithm to traverse the matrix and find the exit point.

To keep track of our current cell position while traversing through a matrix using either of two implemented techniques, we leverage variables x and y. Then, we use loops to move through the matrix based on the priority order: right, down, left, and up.

Move toward approach 1 uses a while loop, and we check the value of every cell utilizing if-else statements. Assuming the ongoing cell is 1, we move to the following cell in the specified direction. In the event that the current cell is 0, we break out of the loop and return the current cell position as the exit point.

Approach 2 uses a do-while loop and a switch statement to handle the cell movement. To efficiently progress through our navigation process, we employ condition-based execution paths that cater specifically to directional movements corresponding with each given current cell value. In essence, when dealing with a current cell that has a value of 1, adjustments are expediently made to accommodate any necessary alterations required in both our x and y coordinate values. Assuming the current cell is 0, we break out of loop.

In the main function, we initialize a binary matrix and call the findExitPoint function to obtain the exit point coordinates. Finally, we print the exit point coordinates using cout.

## Conclusion

An often-encountered programming task is finding an exit point in a binary matrix, which proposes various solution paths. We have delved into two distinct ways implemented in C++ code to tackle this obstacle on this text. Successfully applying these algorithms provides efficient output of identifying where the binary matrix ends or leading towards an endpoint location. Remember to choose the strategy that matches your desired coding style preference and ultimate aim.

Updated on: 25-Jul-2023

49 Views 