-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrones.java
More file actions
265 lines (206 loc) · 6.28 KB
/
drones.java
File metadata and controls
265 lines (206 loc) · 6.28 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Nathan Hicks
* P4 Drones Deliver to Senior Design Groups
* COP 3503
* Uses breadth first search to solve board of drones delivering food to groups
*/
//Imports
import java.util.*;
import java.util.regex.Pattern;
public class drones {
// Directions of movement for swaps.
final public static int[] DX = { -1, 0, 0, 1 };
final public static int[] DY = { 0, -1, 1, 0 };
// Global variables
public static int numDrones;
public static int startHash;
public static int winHash;
public static ArrayList<Drone> drones;
public static ArrayList<Block> blocks;
public static void main(String[] args) {
// Scan in the number of drones
Scanner in = new Scanner(System.in);
numDrones = in.nextInt();
// Create hashmap to hold board positions, arraylists to hold drones and blocks
HashMap<Integer, Integer> grid = new HashMap<Integer, Integer>();
drones = new ArrayList<>();
blocks = new ArrayList<>();
// Add blank drones to arraylist
for (int i = 0; i < numDrones; i++)
drones.add(new Drone(0, 0));
// Scan in grid input checking in pairs
String check = "";
for (int i = 0; i < 64; i++) {
check = in.next(Pattern.compile(".."));
// If drone, update it's position
if (check.startsWith("D")) {
drones.get(Integer.parseInt(check.substring(1)) - 1).setPos(i % 8, i / 8);
}
// If group update target drone and also add to blockers for other drones
if (check.startsWith("G")) {
drones.get(Integer.parseInt(check.substring(1)) - 1).setGoal(i % 8, i / 8);
blocks.add(new Block(i % 8, i / 8));
}
// If blocker add to blockers
if (check.startsWith("X")) {
blocks.add(new Block(i % 8, i / 8));
}
}
in.close(); // Close input
// Adds starting hash value to map and finds winning hash
startHash = hashPos(drones, false);
grid.put(startHash, 0);
winHash = hashPos(drones, true);
// Solve the board
solve(grid);
// If no solution found print -1
if (grid.get(winHash) == null)
System.out.println(-1);
// Print number of moves to get to winning hash
else
System.out.println(grid.get(winHash));
}
// Solves the given board
private static void solve(HashMap<Integer, Integer> grid) {
// Queue for BFS and add starting hash
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer(startHash);
// Loop until queue empty
while (queue.size() != 0) {
// Get the next board.
int val = queue.poll();
int curMoves = grid.get(val);
// Loop through adjacent boards, adding into the queue if they weren't
// previously there.
ArrayList<Integer> next = getNext(val);
for (int i = 0; i < next.size(); i++) {
if (!grid.containsKey(next.get(i))) {
grid.put(next.get(i), curMoves + 1);
// Return early if winning board found
if (grid.containsKey(winHash))
return;
queue.offer(next.get(i));
}
}
}
}
// Finds next possible board positions
private static ArrayList<Integer> getNext(int val) {
// Answer array to be returned
ArrayList<Integer> ans = new ArrayList<Integer>();
// Make copy of board
ArrayList<Drone> copy = makeGrid(val);
// Loops through each direction
for (int i = 0; i < DX.length; i++) {
// Hash result
int res = 0;
// Loops through each drones
for (int j = 0; j < numDrones; j++) {
// Get drone's next position
int swapX = copy.get(j).x + DX[i];
int swapY = copy.get(j).y + DY[i];
// Checks if drone is going to its group, hits its group, or is out of bounds
if (swapX == drones.get(j).goalX && swapY == drones.get(j).goalY) {
// Drone should move if it hits its own group
} else if (!inbounds(swapX, swapY)) {
// Drone doesn't move if out of bounds
swapX = copy.get(j).x;
swapY = copy.get(j).y;
} else if (copy.get(j).x == drones.get(j).goalX && copy.get(j).y == drones.get(j).goalY) {
// Drone doesn't move if it's on it's group
swapX = copy.get(j).x;
swapY = copy.get(j).y;
}
// Builds hash based on its new location
int xShift = j * 6;
int yShift = xShift + 3;
res += (swapX << xShift) + (swapY << yShift);
}
// Add hash value to array
ans.add(res);
}
// Return our list.
return ans;
}
// Makes drone list from hashed value
private static ArrayList<drones.Drone> makeGrid(int val) {
// Result
ArrayList<Drone> res = new ArrayList<>();
// Loop through drones and s position using 111 mask
for (int i = 0; i < numDrones; i++) {
int xShift = i * 6;
int yShift = xShift + 3;
int xPos = (7 << xShift) & val;
int yPos = (7 << yShift) & val;
res.add(new Drone(xPos >> xShift, yPos >> yShift));
}
// Return array
return res;
}
// FInds if position is in bounds
private static boolean inbounds(int swapX, int swapY) {
// Check if position is within grid
if (swapX < 0 || swapX > 7)
return false;
if (swapY < 0 || swapY > 7)
return false;
// Check if position is in block
for (Block b : blocks)
if (b.x == swapX && b.y == swapY)
return false;
// Position is in bounds
return true;
}
// Calculates hash value
private static int hashPos(ArrayList<drones.Drone> drones, boolean goalHash) {
// result to be returned
int res = 0;
// Check if we are calculating hash off of goal positions or current positions
if (!goalHash) {
// Converts drones position into binary
for (int i = 0; i < numDrones; i++) {
int xShift = i * 6;
int yShift = xShift + 3;
res += (drones.get(i).x << xShift) + (drones.get(i).y << yShift);
}
// Return hash
return res;
}
// Converts drones' goal position into binary
for (int i = 0; i < numDrones; i++) {
int xShift = i * 6;
int yShift = xShift + 3;
res += (drones.get(i).goalX << xShift) + (drones.get(i).goalY << yShift);
}
// Return hash
return res;
}
// Drone class for storing position and goal position
static class Drone {
int x, y; // position
int goalX, goalY; // goal position
// Constructor
public Drone(int x, int y) {
this.x = x;
this.y = y;
}
// Sets position
public void setPos(int x, int y) {
this.x = x;
this.y = y;
}
// Sets goal position
public void setGoal(int x, int y) {
goalX = x;
goalY = y;
}
}
// Block class for storing position of blocks
static class Block {
int x, y; // position
// Constructor
public Block(int x, int y) {
this.x = x;
this.y = y;
}
}
}