-
Notifications
You must be signed in to change notification settings - Fork 2
/
FindExitPoint.java
51 lines (38 loc) · 957 Bytes
/
FindExitPoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// User function Template for Java
class Solution {
public int[] FindExitPoint(int n, int m, int[][] matrix) {
// code here
int i = 0, j = 0, dir = 1;
while(i >= 0 && j >= 0 && i < n && j < m){
if(matrix[i][j] == 1){
matrix[i][j] = 0;
dir = (dir % 4) + 1;
}
if(dir == 1){
j++;
}
else if(dir == 2){
i++;
}
else if(dir == 3){
j--;
}
else{
i--;
}
}
if(dir == 1){
j--;
}
else if(dir == 2){
i--;
}
else if(dir == 3){
j++;
}
else{
i++;
}
return new int[] {i,j};
}
}