-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eae03a2
commit 1500ade
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |