-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathShadows-of-the-Knight-Episode-2.java
118 lines (107 loc) Β· 3.09 KB
/
Shadows-of-the-Knight-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
import java.util.*;
import java.io.*;
import java.math.*;
// https://www.codingame.com/training/expert/shadows-of-the-knight-episode-2
class Player {
static Zone warm, cold, current;
static int width, height;
static int x, y;
static int lastX, lastY;
static boolean foundX=false, firstChance=true, outside=false;
static char bombDir = 'U';
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
width = in.nextInt();
height = in.nextInt();
init();
int rounds = in.nextInt();
x = in.nextInt();
y = in.nextInt();
while (true) {
bombDir = in.next().charAt(0);
move();
System.out.println(x+" "+y);
}
}
public static void init() {
current = new Zone(0,width-1);
cold = new Zone(0,width-1);
warm = new Zone(0,width-1);
x = y = lastX = lastY = 0;
}
public static void move() {
int tmpX = x;
int tmpY = y;
switch (bombDir) {
case 'W':
current.update(warm);
break;
case 'C':
current.update(cold);
break;
case 'S':
if (!firstChance) {if (!found()) return;}
break;
}
if (current.low>=current.high) {if (!found()) return;}
firstChance = false;
if (foundX) y = get(y,height-1);
else x = get(x,width-1);
lastX = tmpX;
lastY = tmpY;
}
public static boolean found() {
int tmpX = x;
int tmpY = y;
if (foundX) y = (current.low+current.high)/2;
else {
x = (current.low+current.high)/2;
foundX = true;
current.update(0,height-1);
warm.update(current);
cold.update(current);
}
firstChance = true;
return (x==tmpX&&y==tmpY);
}
public static int get(int value,int limit) {
int low = current.low;
int high = current.high;
int give = low + high - value;
if (outside) {
if (value==0) {give = (give-0)/2;}
else if (value==limit) {give = (limit+give)/2;}
}
outside = false;
if (give==value) give=value+1;
if (give<0) {give = 0; outside = true;}
else if (give>limit) {give = limit; outside = true;}
int middle = (give+value)/2;
int lower = (give+value-1)/2;
int higher = (give+value+1)/2;
if (give>value) {
warm.update(higher,high);
cold.update(low,lower);
}
else if (give<value) {
warm.update(low,lower);
cold.update(higher,high);
}
return give;
}
public static class Zone {
int low, high;
public Zone(int low,int high) {
this.low = low;
this.high = high;
}
public void update(Zone z) {
this.low = z.low;
this.high = z.high;
}
public void update(int low,int high) {
this.low = low;
this.high = high;
}
}
}