-
Notifications
You must be signed in to change notification settings - Fork 3
/
exit_of_matrix.java
57 lines (44 loc) · 908 Bytes
/
exit_of_matrix.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
52
53
54
55
56
57
import java.io.*;
import java.util.*;
public class exit_of_matrix{
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m = scn.nextInt();
int[][] arr = new int[n][m];
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[0].length; j++){
arr[i][j] = scn.nextInt();
}
}
int dir = 0; // e - 0, s - 1, w - 2, n - 3
int i = 0;
int j = 0;
while(true){
dir = (dir + arr[i][j]) % 4;
if(dir == 0){ // east
j++;
}else if(dir == 1){ // south
i++;
}else if(dir == 2){ // west
j--;
}else if(dir == 3){ // north
i--;
}
if( i < 0){
i++;
break;
}else if( j < 0 ){
j++;
break;
}else if( i == arr.length ){
i--;
break;
}else if(j == arr[0].length){
j--;
break;
}
}
System.out.println(i + "\n" + j);
}
}