Given a binary matrix of dimensions N * M, the task is to find the indices of the matrix such that traversal of the given matrix from the cell (0, 0) leads to outside the matrix as per the following conditions:
- If the value of arr[i][j] is 0, then traverse in the same direction and check the next value.
- If the value of arr[i][j] is 1, then update arr[i][j] to 0 and change the current direction from up, right, down, or left to the directions right, down, left, and up respectively.
Examples:
Input: arr[] = {{0, 1}, {1, 0}}
Output: (1, 1)
Explanation:
Below is the image to illustrate the simulation:
Input: arr[] = {{0, 1, 1, 1, 0}, {1, 0, 1, 0, 1}, {1, 1, 1, 0, 0}}
Output: (2, 0)Approach: Follow the steps below to solve the problem:
- Initialize two variables, say current_i and current_j, both as 0.
- Traverse the matrix from the index (0, 0) and set the current direction as a right as R.
- If the value of current_i or current_j is N or M respectively, then perform the following operations:
- If the value of arr[i][j] is 0, then traverse in the same direction and check the next value.
- Otherwise, update arr[i][j] to 0 and choose a direction that is right to the current direction. If the direction which is right to the current direction is:
- L: Move the current row backward i.e., j – 1.
- R: Move the current row forwards i.e., j + 1.
- U: Move the current column upwards i.e., i – 1.
- D: Move the current column downwards i.e., i + 1.
- If the new coordinates are not within the range, then print the current coordinates as the resulting coordinates and break out of the loop.
Below is the implementation of the above approach:
Python3
# Python program for the above approach# Function to check if the indices (i, j)# are valid indices in a Matrix or notdefissafe(m, n, i, j):# Cases for invalid cellsifi <0:returnFalseifj <0:returnFalseifi >=m:returnFalseifj >=n:returnFalse# Return true if validreturnTrue# Function to find indices of cells# of a matrix from which traversal# leads to out of the matrixdefendpoints(arr, m, n):# Starting from cell (0, 0),# traverse in right directioni=0j=0current_d='r'# Stores direction changesrcd={'l':'u','u':'r','r':'d','d':'l'}# Iterate until the current cell# exceeds beyond the matrixwhileissafe(m, n, i, j):# Current indexcurrent_i=icurrent_j=j# If the current cell is 1ifarr[i][j]==1:move_in=rcd[current_d]# Update arr[i][j] = 0arr[i][j]=0# Update indices according# to the directionifmove_in=='u':i-=1elifmove_in=='d':i+=1elifmove_in=='l':j-=1elifmove_in=='r':j+=1current_d=move_in# Otherwiseelse:# Update indices according# to the directionifcurrent_d=='u':i-=1elifcurrent_d=='d':i+=1elifcurrent_d=='l':j-=1elifcurrent_d=='r':j+=1# The exit cooridnatesreturn(current_i, current_j)# Driver Code# Number of rowsM=3# Number of columnsN=5# Given matrix arr[][]arr=[[0,1,1,1,0],[1,0,1,0,1],[1,1,1,0,0],]print(endpoints(arr, M, N))chevron_rightfilter_noneOutput:(2, 0)
Time Complexity: O(R * C)
Auxiliary Space: O(1)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




