-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion3.java
More file actions
78 lines (74 loc) · 2.58 KB
/
Copy pathQuestion3.java
File metadata and controls
78 lines (74 loc) · 2.58 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package practice1_Impl;
public class Question3 {
public int solution(int[][] board){
int n = board.length;
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 2){
x1 = i;
y1 = j;
}
if(board[i][j] == 3){
x2 = i;
y2 = j;
}
}
}
int d1 = 0, d2 = 0, count = 0;
while(count < 10000){
count++;
int nx1 = x1 + dx[d1];
int ny1 = y1 + dy[d1];
int nx2 = x2 + dx[d2];
int ny2 = y2 + dy[d2];
boolean flag1 = true, flag2 = true;
if(nx1 < 0 || nx1 >= n || ny1 < 0 || ny1 >= n || board[nx1][ny1] == 1){
d1 = (d1 + 1) % 4;
flag1 = false;
}
if(nx2 < 0 || nx2 >= n || ny2 < 0 || ny2 >= n || board[nx2][ny2] == 1){
d2 = (d2 + 1) % 4;
flag2 = false;
}
if(flag1 == true){
x1 = nx1;
y1 = ny1;
}
if(flag2 == true){
x2 = nx2;
y2 = ny2;
}
if(x1 == x2 && y1 == y2) break;
}
if(count >= 10000) return 0;
return count;
}
public static void main(String[] args){
Question3 T = new Question3();
int[][] arr1 = {{0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 2, 0, 0},
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 3, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 0, 0, 0}};
System.out.println(T.solution(arr1));
int[][] arr2 = {{1, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 2, 1},
{0, 0, 0, 1, 0, 1, 0, 0, 0, 1},
{0, 1, 0, 1, 0, 0, 0, 0, 0, 3}};
System.out.println(T.solution(arr2));
}
}