forked from hughbzhang/Competitive_Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camelot.java
237 lines (205 loc) · 5.97 KB
/
camelot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package Solved;
/*
ID: bigfish2
LANG: JAVA
TASK: camelot
*/
import java.util.*;
import java.io.*;
import java.awt.*;
public class camelot {
static class search{
int y;
int x;
int value;
public search(int a, int b, int c){
x = a;
y = b;
value = c;
}
}
static int[][] field;//0 = empty 1 = knight 2=king
static ArrayList<Point> knights;
static int[][] dist;//first is knight index second is board index value is distance
static int max;
static int rows;
static int cols;
static int LOC;
static int[] index;
static int[][] kings;
static int[] kingsLOC;
static Point king;
static int walk;
public static void main(String[] args) throws IOException{
BufferedReader f = new BufferedReader(new FileReader("camelot.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("camelot.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
rows = Integer.parseInt(st.nextToken());
cols = Integer.parseInt(st.nextToken());
field = new int[rows][cols];
max = rows*cols;
st = new StringTokenizer(f.readLine());
king = new Point(st.nextToken().charAt(0)-65, Integer.parseInt(st.nextToken())-1);
int KING = convert(king.y,king.x);
field[king.y][king.x] = 2;
knights = new ArrayList<Point>();
LOC = convert(king.y, king.x);//king location
kings = new int[49][max];
kingsLOC = new int[49];
String temp = f.readLine();
while(temp!=null){
st = new StringTokenizer(temp);
while(st.hasMoreTokens()){
Point a = new Point(st.nextToken().charAt(0)-65, Integer.parseInt(st.nextToken())-1);
knights.add(a);
field[a.y][a.x] = 1;
}
temp = f.readLine();
}
System.out.println("start");
dist = new int[knights.size()][max];
index = new int[knights.size()];
for(int a = 0;a<kingsLOC.length;a++){
int up = (int)(a/7);
int left = a%7;
int y = king.y-3+up;
int x = king.x-3+left;
kingsLOC[a] = convert(y,x);
if(y>=rows||y<0||x>=cols||x<0) kingsLOC[a] = -1;
}
for(int x = 0;x<index.length;x++){
index[x] = convert(knights.get(x).y, knights.get(x).x);
}
for(int y = 0; y<knights.size();y++){
for(int x = 0;x<max;x++){
dist[y][x] = Integer.MAX_VALUE;
}
}
for(int y = 0; y<49;y++){
for(int x = 0;x<max;x++){
kings[y][x] = Integer.MAX_VALUE;
}
}
for(int x = 0;x<knights.size();x++){
fill(x, knights.get(x).y, knights.get(x).x, dist, 0);
}
System.out.println("half");
for(int x = 0;x<kingsLOC.length;x++){
int a = kingsLOC[x];
if(a==-1) continue;
int start = kingDist(a, KING);
fill(x, (int)(a/cols), a%cols, kings, start);
}
System.out.println("last");
/*for(int x = 0;x<max;x++){
if(x%cols==0) System.out.println();
System.out.print(dist[0][x]+" ");
}*/
int cur = Integer.MAX_VALUE;
//int debug = -1;
for(int x = 0;x<max;x++){//destination
//base case king walks there himself
int base = base(x);
if(base==-1) continue;
if(cur>base) cur = base;
base-=walk;
int margin = cur-base;
if(margin<=0) continue;
for(int y = 0;y<knights.size();y++){//which knight picks up the king
for(int a = 0;a<49;a++){
if(kingsLOC[a]==-1) continue;
if(dist[y][kingsLOC[a]]==Integer.MAX_VALUE) continue;
if(kings[a][x]==Integer.MAX_VALUE) continue;
if(dist[y][x]==Integer.MAX_VALUE) continue;
int pos = dist[y][kingsLOC[a]]+kings[a][x]-dist[y][x];
if(pos<margin){
// debug = a;
margin = pos;
}
}
}
if(cur>margin+base){
cur = margin+base;
//System.out.println(cur+" "+debug);
}
}
out.println(cur);
f.close();
out.close();
System.exit(0);
}
static int base(int a){
int answer = 0;
for(int x = 0;x<knights.size();x++){
if(dist[x][a]==Integer.MAX_VALUE) return -1;
answer+=dist[x][a];
}
Point dest = revert(a);
walk = Math.max(Math.abs(dest.x-king.x),Math.abs(dest.y-king.y));
answer+=walk;
return answer;
}
static int kingDist(int a, int b){
Point one = revert(a);
Point two = revert(b);
return Math.max(Math.abs(one.x-two.x),Math.abs(one.y-two.y));
}
static void print(int[][] field){
for(int y = 0;y<field.length;y++){
for(int x = 0;x<field[0].length;x++){
System.out.print(field[y][x]);
}
System.out.println();
}
}
/*static void fill(int knight, int value, int y, int x, int[][] dist){
if(y<0||y>=rows||x<0||x>=cols) return; //if outside board return
if(dist[knight][y*cols+x]>value) dist[knight][y*cols+x] = value;//fill in value
else return;
value++;
fill(knight, value, y+1, x+2, dist);
fill(knight, value, y+1, x-2, dist);
fill(knight, value, y-1, x+2, dist);
fill(knight, value, y-1, x-2, dist);
fill(knight, value, y-2, x+1, dist);
fill(knight, value, y-2, x-1, dist);
fill(knight, value, y+2, x+1, dist);
fill(knight, value, y+2, x-1, dist);
//send to 8 next spots
return;
}*/
static void fill(int knight, int y, int x, int[][] dist, int start){
if(y<0||y>=rows||x<0||x>=cols) return; //if outside board return
Queue<search> list = new LinkedList<search>();//x is the location y is the value
list.add(new search(x,y,start));
while(!list.isEmpty()){
search cur = list.poll();
x = cur.x;
y = cur.y;
if(y<0||y>=rows||x<0||x>=cols) continue; //if outside board return
if(dist[knight][y*cols+x]>cur.value) dist[knight][y*cols+x] = cur.value;//fill in value
else continue;
int value = cur.value+1;
list.offer(new search(x+2,y+1, value));
list.offer(new search(x+2,y-1, value));
list.offer(new search(x-2,y+1, value));
list.offer(new search(x-2,y-1, value));
list.offer(new search(x+1,y+2, value));
list.offer(new search(x+1,y-2, value));
list.offer(new search(x-1,y+2, value));
list.offer(new search(x-1,y-2, value));
}
}
static int carry(int knight, int dest, int y, int x){
int king = convert(y, x);
return dist[knight][king]+dist[king][dest];
}
static int convert(int y, int x){
return y*cols+x;
}
static Point revert(int loc){
int y = (int)(loc/cols);
int x = loc%cols;
return new Point(x,y);
}
}