Skip to content

Commit

Permalink
Create Exit point in a matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Apr 26, 2024
1 parent eae03a2 commit 1500ade
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions Exit point in a matrix
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//Exit point in a matrix

import java.io.*;
import java.lang.*;
import java.util.*;

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());

while(T-- > 0) {
String[] s = br.readLine().trim().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int[][] matrix = new int[n][m];

for(int i = 0; i < n; i++) {
String[] S = br.readLine().split(" ");

for(int j = 0; j < m; j++) {
matrix[i][j] = Integer.parseInt(S[j]);
}
}

Solution ob = new Solution();
int[] ans = ob.FindExitPoint(n, m, matrix);

for(int i = 0; i < ans.length; i++)
System.out.print(ans[i] + " ");
System.out.println();
}
}
}

class Solution {
public int[] FindExitPoint(int n, int m, int[][] matrix) {
int[] ans = new int[2];
int i = 0, j = 0, count = 0;
int previ = 0, prevj = 0;

while(j > -1 && j < m && i > -1 && i < n) {
previ = i;
prevj = j;

if(matrix[i][j] == 1) {
matrix[i][j] = 0;

if(count == 0) {
i++;
count = 1;
}

else if(count == 1) {
j--;
count = 2;
}

else if(count == 2) {
i--;
count = 3;
}

else if(count == 3) {
count = 0;
j++;
}
}

else {
if(count == 0) {
j++;
}

else if(count == 1) {
i++;
}

else if(count == 2) {
j--;
}

else {
i--;
}
}
}

ans[0] = previ;
ans[1] = prevj;

return ans;
}
}

0 comments on commit 1500ade

Please sign in to comment.