-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDon't-Panic-Episode-2.java
141 lines (123 loc) Β· 4.63 KB
/
Don't-Panic-Episode-2.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
135
136
137
138
139
140
141
import java.util.*;
import java.awt.Point;
// https://www.codingame.com/training/hard/don't-panic-episode-2
class Player {
int width, height;
int clones, elevators;
Moves[] moves;
int rounds;
boolean checked[][][][][];
boolean lift[][];
int EX, EY;
public static void main(String[] args) {
Player game = new Player();
Scanner in = new Scanner(System.in);
game.init(in);
while (true) {game.play(in);}
}
void init(Scanner in) {
height = in.nextInt();
width = in.nextInt();
rounds = in.nextInt()+1;
EY = in.nextInt();
EX = in.nextInt();
clones = in.nextInt();
elevators = in.nextInt();
lift = new boolean[width][height];
int lifts = in.nextInt();
for (int i = 0; i < lifts; i++) {
int y = in.nextInt();
int x = in.nextInt();
lift[x][y] = true;
}
}
void play(Scanner in) {
int y = in.nextInt();
int x = in.nextInt();
String direction = in.next();
int dir = 0;
switch (direction) {
case "LEFT": dir = 0; break;
case "RIGHT": dir = 1; break;
}
Unit cnt = new Unit(x,y,clones,elevators,dir,"WAIT");
if (x == -1) {System.out.println("WAIT");}
else {
String move = getMove(cnt);
execute(move);
System.out.println(move);
}
rounds--;
}
void execute(String move) {
switch (move) {
case "ELEVATOR": clones--; elevators--; break;
case "BLOCK": clones--; break;
}
}
String getMove(Unit now) {
checked = new boolean[width][height][clones+1][elevators+1][2];
moves = new Moves[rounds];
for (int i = 0; i < rounds; i++) {moves[i] = new Moves();}
moves[0].add(now);
checked[now.x][now.y][now.clones][now.lifts][now.dir] = true;
for (int time = 0; time < rounds; time++) {
//System.err.println("Time = "+time+":");
while (moves[time].size() > 0) {
Unit cnt = moves[time].get(0);
if (cnt.y == EY && cnt.x == EX) {
System.err.println("Reached exit at: "+time);
return cnt.parent;
}
addAll(cnt,time);
moves[time].remove(0);
}
}
return "WAIT";
}
void addAll(Unit cnt,int time) {
boolean canBlock = cnt.clones > 0;
boolean canLift = cnt.lifts > 0 && canBlock && cnt.y < EY;
int dir = cnt.dir == 0? -1 : 1;
int nxt = cnt.x + dir;
int hyp = cnt.x - dir;
if (!lift[cnt.x][cnt.y] && inRange(hyp) && time < rounds-5 && canBlock && !checked[hyp][cnt.y][cnt.clones-1][cnt.lifts][1-cnt.dir]) {
String move = time == 0? "BLOCK" : cnt.parent;
moves[time+4].add(new Unit(hyp,cnt.y,cnt.clones-1,cnt.lifts,1-cnt.dir,move));
clearAll(hyp,cnt.y,cnt.clones-1,cnt.lifts,1-cnt.dir);
}
if (!lift[cnt.x][cnt.y] && time < rounds-5 && canLift && !checked[cnt.x][cnt.y+1][cnt.clones-1][cnt.lifts-1][cnt.dir]) {
String move = time == 0? "ELEVATOR" : cnt.parent;
moves[time+4].add(new Unit(cnt.x,cnt.y+1,cnt.clones-1,cnt.lifts-1,cnt.dir,move));
clearAll(cnt.x,cnt.y+1,cnt.clones-1,cnt.lifts-1,cnt.dir);
}
if (lift[cnt.x][cnt.y] && time < rounds-2 && !checked[cnt.x][cnt.y+1][cnt.clones][cnt.lifts][cnt.dir]) {
String move = time == 0? "WAIT" : cnt.parent;
moves[time+1].add(new Unit(cnt.x,cnt.y+1,cnt.clones,cnt.lifts,cnt.dir,move));
clearAll(cnt.x,cnt.y+1,cnt.clones,cnt.lifts,cnt.dir);
}
if (!lift[cnt.x][cnt.y] && inRange(nxt) && time < rounds-2 && !checked[nxt][cnt.y][cnt.clones][cnt.lifts][cnt.dir]) {
String move = time == 0? "WAIT" : cnt.parent;
moves[time+1].add(new Unit(nxt,cnt.y,cnt.clones,cnt.lifts,cnt.dir,move));
clearAll(nxt,cnt.y,cnt.clones,cnt.lifts,cnt.dir);
}
}
void clearAll(int x,int y,int clones,int lifts,int dir) {
for (int i = clones; i >= 0; i--) {
checked[x][y][clones][lifts][dir] = true;
}
}
boolean inRange(int val) {return val >= 0 && val < width;}
class Moves extends ArrayList<Unit> {}
class Unit extends Point {
int clones, lifts, dir;
String parent;
Unit(int x,int y,int clones,int lifts,int dir,String parent) {
super(x,y);
this.clones = clones;
this.lifts = lifts;
this.dir = dir;
this.parent = parent;
}
}
}