@@ -102,8 +102,7 @@ class Solution:
102
102
return - 1
103
103
```
104
104
105
- ``` Java
106
-
105
+ ``` java
107
106
class Solution {
108
107
public int findMinStep (String board , String hand ) {
109
108
final Zuma zuma = Zuma . create(board, hand);
@@ -116,29 +115,27 @@ class Solution {
116
115
}
117
116
118
117
private int bfs (ArrayList<Zuma > curr , int k , HashSet<Long > visited ) {
119
- if (curr. isEmpty())
118
+ if (curr. isEmpty()) {
120
119
return - 1 ;
120
+ }
121
121
122
122
final ArrayList<Zuma > next = new ArrayList<> ();
123
123
124
124
for (Zuma zuma : curr) {
125
125
ArrayList<Zuma > neib = zuma. getNextLevel(k, visited);
126
- if (neib == null )
126
+ if (neib == null ) {
127
127
return k + 1 ;
128
+ }
128
129
129
130
next. addAll(neib);
130
131
}
131
132
return bfs(next, k + 1 , visited);
132
133
}
133
134
}
134
135
135
-
136
136
record Zuma(long board, long hand) {
137
137
public static Zuma create(String boardStr, String handStr) {
138
- return new Zuma (
139
- Zuma . encode(boardStr, false ),
140
- Zuma . encode(handStr, true )
141
- );
138
+ return new Zuma (Zuma . encode(boardStr, false ), Zuma . encode(handStr, true ));
142
139
}
143
140
144
141
public ArrayList<Zuma > getNextLevel(int depth, HashSet<Long > visited) {
@@ -150,15 +147,18 @@ record Zuma(long board, long hand) {
150
147
for (long [] pair : handList) {
151
148
for (int i = 0 ; i < size; ++ i) {
152
149
final long rawBoard = pruningCheck(boardList[i], pair[0 ], i * 3 , depth);
153
- if (rawBoard == - 1 )
150
+ if (rawBoard == - 1 ) {
154
151
continue ;
152
+ }
155
153
156
154
final long nextBoard = updateBoard(rawBoard);
157
- if (nextBoard == 0 )
155
+ if (nextBoard == 0 ) {
158
156
return null ;
157
+ }
159
158
160
- if (pair[1 ] == 0 || visited. contains(nextBoard))
159
+ if (pair[1 ] == 0 || visited. contains(nextBoard)) {
161
160
continue ;
161
+ }
162
162
163
163
visited. add(nextBoard);
164
164
next. add(new Zuma (nextBoard, pair[1 ]));
@@ -171,8 +171,7 @@ record Zuma(long board, long hand) {
171
171
final long L = (insBoard >> (pos + 3 )) & 0x7 ;
172
172
final long R = (insBoard >> (pos - 3 )) & 0x7 ;
173
173
174
- if (depth == 0 && (ball != R ) && (L != R ) ||
175
- depth > 0 && (ball != R )) {
174
+ if (depth == 0 && (ball != R ) && (L != R ) || depth > 0 && (ball != R )) {
176
175
return - 1 ;
177
176
}
178
177
return insBoard | (ball << pos);
@@ -183,21 +182,20 @@ record Zuma(long board, long hand) {
183
182
184
183
for (int i = 0 ; i < 64 ; i += 3 ) {
185
184
final long curr = (board >> i) & 0x7 ;
186
- final long top = (stack) & 0x7 ;
185
+ final long top = (stack) & 0x7 ;
187
186
188
187
// pop (if possible)
189
- if ((top > 0 ) && (curr != top) &&
190
- (stack & 0x3F ) == ((stack >> 3 ) & 0x3F ))
191
- {
188
+ if ((top > 0 ) && (curr != top) && (stack & 0x3F ) == ((stack >> 3 ) & 0x3F )) {
192
189
stack >> = 9 ;
193
- if ((stack & 0x7 ) == top)
194
- stack >> = 3 ;
190
+ if ((stack & 0x7 ) == top) stack >> = 3 ;
195
191
}
196
192
197
- if (curr == 0 )
198
- break ; // done
199
- else
200
- stack = (stack << 3 ) | curr; // push and continue
193
+ if (curr == 0 ) {
194
+ // done
195
+ break ;
196
+ }
197
+ // push and continue
198
+ stack = (stack << 3 ) | curr;
201
199
}
202
200
return stack;
203
201
}
@@ -209,15 +207,14 @@ record Zuma(long board, long hand) {
209
207
210
208
for (int i = 0 ; i < 16 ; i += 3 ) {
211
209
final long currBall = (this . hand >> i) & 0x7 ;
212
- if (currBall == 0 )
210
+ if (currBall == 0 ) {
213
211
break ;
212
+ }
214
213
215
214
if (currBall != prevBall) {
216
215
prevBall = currBall;
217
- handList. add(new long []{
218
- currBall,
219
- ((this . hand >> 3 ) & ~ ballMask) | (this . hand & ballMask)
220
- });
216
+ handList. add(
217
+ new long [] {currBall, ((this . hand >> 3 ) & ~ ballMask) | (this . hand & ballMask)});
221
218
}
222
219
ballMask = (ballMask << 3 ) | 0x7 ;
223
220
}
@@ -232,8 +229,9 @@ record Zuma(long board, long hand) {
232
229
233
230
while (true ) {
234
231
final long currBall = this . board & ballMask;
235
- if (currBall == 0 )
232
+ if (currBall == 0 ) {
236
233
break ;
234
+ }
237
235
238
236
ballMask << = 3 ;
239
237
insBoard = (insBoard | currBall) & ~ ballMask;
@@ -244,8 +242,9 @@ record Zuma(long board, long hand) {
244
242
245
243
private static long encode(String stateStr, boolean sortFlag) {
246
244
final char [] stateChars = stateStr. toCharArray();
247
- if (sortFlag)
245
+ if (sortFlag) {
248
246
Arrays . sort(stateChars);
247
+ }
249
248
250
249
long stateBits = 0 ;
251
250
for (char ch : stateChars) {
@@ -255,7 +254,7 @@ record Zuma(long board, long hand) {
255
254
}
256
255
257
256
private static long encode(char ch) {
258
- return switch (ch) {
257
+ return switch (ch) {
259
258
case ' R' - > 0x1 ;
260
259
case ' G' - > 0x2 ;
261
260
case ' B' - > 0x3 ;
0 commit comments