-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1037. Lara Croft.java
134 lines (103 loc) · 3.16 KB
/
1037. Lara Croft.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Scanner;
/* Not the best solution, but it works (simulating FSM) */
public class LaraCroft {
public static class Lara {
private static enum State{
LEFT, RIGHT, UP, DOWN
}
public int i = 1; /* coordinates */
public int j = 1;
public State state = State.RIGHT;
public void turn() {
switch (state) {
case LEFT:
state = State.UP;
break;
case RIGHT:
state = State.DOWN;
break;
case UP:
state = State.RIGHT;
break;
case DOWN:
state = State.LEFT;
break;
}
}
public void move() {
switch (state) {
case LEFT:
j = j - 1;
break;
case RIGHT:
j = j + 1;
break;
case UP:
i = i - 1;
break;
case DOWN:
i = i + 1;
break;
}
}
}
public static class Graveyard {
public Lara lara;
public byte graves[][];
public Graveyard(int N, int M) {
graves = new byte[N + 2][M + 2];
for (int i = 0; i <= N + 1; i++) {
graves[i][0] = 1;
graves[i][M + 1] = 1;
}
for (int i = 0; i <= M + 1; i++) {
graves[0][i] = 1;
graves[N + 1][i] = 1;
}
lara = new Lara();
graves[1][1] = 1;
}
public boolean canMove() {
switch (lara.state) {
case LEFT:
return graves[lara.i][lara.j - 1] == 0;
case RIGHT:
return graves[lara.i][lara.j + 1] == 0;
case UP:
return graves[lara.i - 1][lara.j] == 0;
case DOWN:
return graves[lara.i + 1][lara.j] == 0;
}
return false;
}
}
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int i1 = sc.nextInt();
int j1 = sc.nextInt();
int i2 = sc.nextInt();
int j2 = sc.nextInt();
Graveyard graveyard = new Graveyard(N, M);
int days = 0;
int day1 = -1;
int day2 = -1;
while (day1 == -1 || day2 == -1) {
if (graveyard.lara.i == i1 && graveyard.lara.j == j1) {
day1 = days;
}
if (graveyard.lara.i == i2 && graveyard.lara.j == j2) {
day2 = days;
}
if (graveyard.canMove()) {
days++;
graveyard.lara.move();
graveyard.graves[graveyard.lara.i][graveyard.lara.j] = 1;
} else {
graveyard.lara.turn();
}
}
System.out.println(Math.abs(day1 - day2));
}
}