Skip to content

Commit 4819061

Browse files
authored
Gregory the Grasshopper kattis
1 parent 65d1950 commit 4819061

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by AMK on 8/2/2019.
5+
* Life is nice :)
6+
* Enjoy coding :D
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Scanner scanner = new Scanner(System.in);
11+
while (scanner.hasNext()) {
12+
int R, C, GR, GC, LR, LC;
13+
String line[] = scanner.nextLine().split(" ");
14+
R = Integer.parseInt(line[0]);
15+
C = Integer.parseInt(line[1]);
16+
GR = Integer.parseInt(line[2]);
17+
GC = Integer.parseInt(line[3]);
18+
LR = Integer.parseInt(line[4]);
19+
LC = Integer.parseInt(line[5]);
20+
Queue<Pair> queue = new LinkedList<>();
21+
int arr[][] = new int[101][101];
22+
queue.add(new Pair(GR,GC, 0));
23+
boolean flag = true;
24+
while (!queue.isEmpty()){
25+
Pair loc = queue.poll();
26+
if (arr[loc.x][loc.y] != 0){
27+
continue;
28+
}
29+
arr[loc.x][loc.y] = 1;
30+
if (loc.y == LC && loc.x == LR){
31+
System.out.println(loc.counter);
32+
flag = false;
33+
break;
34+
}
35+
if (loc.y - 2>= 1){
36+
if (loc.x - 1>= 1){
37+
queue.add(new Pair(loc.x - 1, loc.y - 2,loc.counter + 1));
38+
}
39+
if (loc.x + 1 <= R){
40+
queue.add(new Pair(loc.x + 1, loc.y - 2,loc.counter + 1));
41+
}
42+
}
43+
if (loc.y + 2 <= C){
44+
if (loc.x - 1>= 1){
45+
queue.add(new Pair(loc.x - 1, loc.y + 2,loc.counter + 1));
46+
}
47+
if (loc.x + 1 <= R){
48+
queue.add(new Pair(loc.x + 1, loc.y + 2,loc.counter + 1));
49+
}
50+
}
51+
if (loc.y - 1>= 1){
52+
if (loc.x - 2>= 1){
53+
queue.add(new Pair(loc.x - 2, loc.y - 1,loc.counter + 1));
54+
}
55+
if (loc.x + 2 <= R){
56+
queue.add(new Pair(loc.x + 2, loc.y - 1,loc.counter + 1));
57+
}
58+
}
59+
if (loc.y + 1 <= C){
60+
if (loc.x - 2 >= 1){
61+
queue.add(new Pair(loc.x - 2, loc.y + 1,loc.counter + 1));
62+
}
63+
if (loc.x + 2 <= R){
64+
queue.add(new Pair(loc.x + 2, loc.y + 1,loc.counter + 1));
65+
}
66+
}
67+
}
68+
if (flag)
69+
System.out.println("impossible");
70+
}
71+
}
72+
static class Pair{
73+
int x,y,counter;
74+
75+
Pair(int x, int y, int counter) {
76+
this.x = x;
77+
this.y = y;
78+
this.counter = counter;
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)