-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
51 lines (39 loc) · 723 Bytes
/
Knight.java
File metadata and controls
51 lines (39 loc) · 723 Bytes
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
package codility;
public class Knight {
public int solution(int A, int B) {
int temp;
if( A < 0)
A= -A;
if( B < 0)
B= -B;
if( A > B) {
temp = A;
A= B;
B= temp;
}
if( B == 2*A)
return A;
if( A == B) {
if( A % 3 == 0)
return 2*(A/3);
if( A % 3 == 1)
return 2+2*(A-1)/3;
if( A % 3 == 1)
return 4+2*(A-2)/3;
}
if(A == 0) {
if( B % 4 == 0)
return B/2;
if( B % 4 == 1 || B % 4 == 3)
return 3+(B -(B % 4))/2;
if( B % 4 == 2)
return 2+(B-2)/2;
}
if( B > 2*A)
return solution(2*A-B,2*A-B) + solution(B-A,2*(B-A));
else
return solution(0,B-2*A) + solution(A,2*A);
}
public static void main(String args[]) {
}
}