-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path864.java
More file actions
270 lines (223 loc) · 9.21 KB
/
864.java
File metadata and controls
270 lines (223 loc) · 9.21 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
266
267
268
269
270
__________________________________________________________________________________________________
sample 11 ms submission
/*
"@...a"
".###A"
"b.BCc"
*/
class Solution {
private static final int NO_SOLUTION = -1;
private static final char WALL = '#';
private static final char START = '@';
private static final int[][] MOVES = new int[][] {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
private boolean[][][] discovered;
private Queue<State> queue;
private String[] grid;
private int m, n;
private int goalKeys;
private static boolean isKey(char c) {
return c >= 'a' && c <= 'f';
}
private static boolean isLock(char c) {
return c >= 'A' && c <= 'F';
}
private static int addKey(int keys, char key) {
return keys | (1 << ((int)key - 'a'));
}
// returns true if goal state is reached
private boolean queueNeighbors(State from) {
boolean goalReached = false;
for (int[] move : MOVES) {
int x = from.x() + move[0];
int y = from.y() + move[1];
if (x < 0 || y < 0 || x >= m || y >= n) continue;
char c = grid[x].charAt(y);
if (c == WALL) continue;
if (isLock(c) && !from.hasKey(c)) continue;
if (discovered[x][y][from.keys()]) continue;
int neighborKeys = isKey(c) ? addKey(from.keys(), c) : from.keys();
discovered[x][y][from.keys()] = true;
queue.offer(new State(neighborKeys, x, y));
goalReached |= neighborKeys == goalKeys;
}
return goalReached;
}
public int shortestPathAllKeys(String[] grid) {
if (grid == null || grid.length < 1 || grid[0].length() < 1) return 0;
m = grid.length;
n = grid[0].length();
discovered = new boolean[m][n][64];
this.grid = grid;
int x = 0, y = 0;
for (int i = 0; grid.length > i; i++) {
String row = grid[i];
for (int j = 0; row.length() > j; j++) {
char c = row.charAt(j);
if (c == START) {
x = i;
y = j;
} else if (isKey(c)) {
goalKeys = addKey(goalKeys, c);
}
}
}
if (goalKeys == 0) return 0;
queue = new LinkedList<>();
queue.offer(new State(0, x, y));
discovered[x][y][0] = true;
int moveCount = 0;
while (!queue.isEmpty()) {
moveCount++;
int qSize = queue.size();
while (qSize-- > 0) {
State current = queue.poll();
if (queueNeighbors(current)) {
return moveCount;
}
}
}
return NO_SOLUTION;
}
class State {
private int keys;
private int x;
private int y;
State(int keys, int x, int y) {
this.keys = keys;
this.x = x;
this.y = y;
}
public boolean hasKey(char lock) {
return ((keys >> ((int)lock - 'A')) & 1) == 1;
}
public int keys() {
return keys;
}
public int x() {
return x;
}
public int y() {
return y;
}
}
}
__________________________________________________________________________________________________
sample 37888 kb submission
import java.awt.Point;
class Solution {
int[][] dirs = {{1,0},{0,1},{-1,0},{0,-1}};
private int[][] bfs(int i, int j, String[] grid) {
int[][] dist = new int[grid.length][grid[0].length()];
boolean[][] visited = new boolean[grid.length][grid[0].length()];
Queue<Point> queue = new ArrayDeque<>();
for (int ii=0;ii<grid.length;ii++) {
for (int jj=0;jj<grid[ii].length();jj++) {
dist[ii][jj]=Integer.MAX_VALUE;
}
}
visited[i][j] = true;
dist[i][j] = 0;
queue.add(new Point(i,j));
while (!queue.isEmpty()) {
Point cur = queue.poll();
int curI = cur.x;
int curJ = cur.y;
for (int k=0;k<dirs.length;k++) {
int newI = curI + dirs[k][0];
int newJ = curJ + dirs[k][1];
if (newI >= 0 && newJ >= 0 &&
newI < grid.length && newJ < grid[0].length() &&
!visited[newI][newJ] &&
grid[newI].charAt(newJ) != '#'
) {
dist[newI][newJ]=dist[curI][curJ]+1;
visited[newI][newJ]=true;
if ( grid[newI].charAt(newJ) == '.' || grid[newI].charAt(newJ) == '@') {
queue.add(new Point(newI,newJ));
}
}
}
}
return dist;
}
public int shortestPathAllKeys(String[] grid) {
HashMap<Character,Point> indexer = new HashMap<>();
int allKeysMask=0;
for (int i=0;i<grid.length;i++) {
for (int j=0;j<grid[i].length();j++) {
char c = grid[i].charAt(j);
if (c != '.' && c != '#') {
indexer.put(c,new Point(i,j));
}
if (Character.isLowerCase(c)) {
allKeysMask |= (1 << (c - 'a'));
}
}
}
HashMap<Character,HashMap<Character,Integer>> dists = new HashMap<>();
for (char startPlace : indexer.keySet()) {
dists.putIfAbsent(startPlace,new HashMap<Character,Integer>());
int startI = indexer.get(startPlace).x;
int startJ = indexer.get(startPlace).y;
int[][] dArr = bfs(startI,startJ,grid);
for (char destPlace : indexer.keySet()) {
int endI = indexer.get(destPlace).x;
int endJ = indexer.get(destPlace).y;
dists.get(startPlace).put(destPlace,dArr[endI][endJ]);
}
}
//System.out.println("dists "+dists);
//dijkstra
HashMap<String,Integer> finalDist = new HashMap<>();
HashSet<String> visited = new HashSet<>();
PriorityQueue<String> pq = new PriorityQueue<>((a,b)->{
int d= finalDist.getOrDefault(a,Integer.MAX_VALUE).compareTo(finalDist.getOrDefault(b,Integer.MAX_VALUE));
if (d != 0) return d;
return a.compareTo(b);
});
pq.add(("@"+0));
visited.add(("@"+0));
finalDist.put(("@"+0),0);
while (!pq.isEmpty()) {
String node = pq.poll();
visited.add(node);
//System.out.println("node "+node);
char c = node.charAt(0);
int nodeState = Integer.valueOf(node.substring(1));
int d = finalDist.getOrDefault(node,Integer.MAX_VALUE);
// System.out.println("d "+d);
if (c == '*') return finalDist.getOrDefault(String.valueOf('*')+allKeysMask,Integer.MAX_VALUE);
if (nodeState == allKeysMask) { //another nei (last node)
char dest = '*';
int newState = nodeState;
if (d < finalDist.getOrDefault(String.valueOf(dest)+newState, Integer.MAX_VALUE)) {
pq.remove(String.valueOf(dest)+newState);
finalDist.put(String.valueOf(dest)+newState,d);
pq.add(String.valueOf(dest)+newState);
}
}
for (char dest : dists.get(c).keySet()) {
int newState = nodeState;
int dToDest = dists.get(c).get(dest);
if (Character.isLowerCase(dest)) //key
newState |= (1 << (dest - 'a'));
if (Character.isUpperCase(dest)) //lock
if ((nodeState & (1 << (dest - 'A'))) == 0 ) continue; //no key
if (visited.contains(String.valueOf(dest)+newState)) continue;
if (dToDest != Integer.MAX_VALUE &&
dToDest + d < finalDist.getOrDefault(String.valueOf(dest)+newState, Integer.MAX_VALUE)) {
pq.remove(String.valueOf(dest)+newState);
finalDist.put(String.valueOf(dest)+newState,dToDest + d);
pq.add(String.valueOf(dest)+newState);
}
}
}
return -1;
}
}
__________________________________________________________________________________________________