Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add java solution to lc problem: No.0488 #2430

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions solution/0400-0499/0488.Zuma Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,172 @@ class Solution:
return -1
```

```java
class Solution {
public int findMinStep(String board, String hand) {
final Zuma zuma = Zuma.create(board, hand);
final HashSet<Long> visited = new HashSet<>();
final ArrayList<Zuma> init = new ArrayList<>();

visited.add(zuma.board());
init.add(zuma);
return bfs(init, 0, visited);
}

private int bfs(ArrayList<Zuma> curr, int k, HashSet<Long> visited) {
if (curr.isEmpty()) {
return -1;
}

final ArrayList<Zuma> next = new ArrayList<>();

for (Zuma zuma : curr) {
ArrayList<Zuma> neib = zuma.getNextLevel(k, visited);
if (neib == null) {
return k + 1;
}

next.addAll(neib);
}
return bfs(next, k + 1, visited);
}
}

record Zuma(long board, long hand) {
public static Zuma create(String boardStr, String handStr) {
return new Zuma(Zuma.encode(boardStr, false), Zuma.encode(handStr, true));
}

public ArrayList<Zuma> getNextLevel(int depth, HashSet<Long> visited) {
final ArrayList<Zuma> next = new ArrayList<>();
final ArrayList<long[]> handList = this.buildHandList();
final long[] boardList = new long[32];
final int size = this.buildBoardList(boardList);

for (long[] pair : handList) {
for (int i = 0; i < size; ++i) {
final long rawBoard = pruningCheck(boardList[i], pair[0], i * 3, depth);
if (rawBoard == -1) {
continue;
}

final long nextBoard = updateBoard(rawBoard);
if (nextBoard == 0) {
return null;
}

if (pair[1] == 0 || visited.contains(nextBoard)) {
continue;
}

visited.add(nextBoard);
next.add(new Zuma(nextBoard, pair[1]));
}
}
return next;
}

private long pruningCheck(long insBoard, long ball, int pos, int depth) {
final long L = (insBoard >> (pos + 3)) & 0x7;
final long R = (insBoard >> (pos - 3)) & 0x7;

if (depth == 0 && (ball != R) && (L != R) || depth > 0 && (ball != R)) {
return -1;
}
return insBoard | (ball << pos);
}

private long updateBoard(long board) {
long stack = 0;

for (int i = 0; i < 64; i += 3) {
final long curr = (board >> i) & 0x7;
final long top = (stack) &0x7;

// pop (if possible)
if ((top > 0) && (curr != top) && (stack & 0x3F) == ((stack >> 3) & 0x3F)) {
stack >>= 9;
if ((stack & 0x7) == top) stack >>= 3;
}

if (curr == 0) {
// done
break;
}
// push and continue
stack = (stack << 3) | curr;
}
return stack;
}

private ArrayList<long[]> buildHandList() {
final ArrayList<long[]> handList = new ArrayList<>();
long prevBall = 0;
long ballMask = 0;

for (int i = 0; i < 16; i += 3) {
final long currBall = (this.hand >> i) & 0x7;
if (currBall == 0) {
break;
}

if (currBall != prevBall) {
prevBall = currBall;
handList.add(
new long[] {currBall, ((this.hand >> 3) & ~ballMask) | (this.hand & ballMask)});
}
ballMask = (ballMask << 3) | 0x7;
}
return handList;
}

private int buildBoardList(long[] buffer) {
int ptr = 0;
long ballMask = 0x7;
long insBoard = this.board << 3;
buffer[ptr++] = insBoard;

while (true) {
final long currBall = this.board & ballMask;
if (currBall == 0) {
break;
}

ballMask <<= 3;
insBoard = (insBoard | currBall) & ~ballMask;
buffer[ptr++] = insBoard;
}
return ptr;
}

private static long encode(String stateStr, boolean sortFlag) {
final char[] stateChars = stateStr.toCharArray();
if (sortFlag) {
Arrays.sort(stateChars);
}

long stateBits = 0;
for (char ch : stateChars) {
stateBits = (stateBits << 3) | Zuma.encode(ch);
}
return stateBits;
}

private static long encode(char ch) {
return switch (ch) {
case 'R' -> 0x1;
case 'G' -> 0x2;
case 'B' -> 0x3;
case 'W' -> 0x4;
case 'Y' -> 0x5;
case ' ' -> 0x0;
default ->
throw new IllegalArgumentException("Invalid char: " + ch);
};
}
}
```

<!-- tabs:end -->

<!-- end -->
166 changes: 166 additions & 0 deletions solution/0400-0499/0488.Zuma Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,172 @@ class Solution:
return -1
```

```java
class Solution {
public int findMinStep(String board, String hand) {
final Zuma zuma = Zuma.create(board, hand);
final HashSet<Long> visited = new HashSet<>();
final ArrayList<Zuma> init = new ArrayList<>();

visited.add(zuma.board());
init.add(zuma);
return bfs(init, 0, visited);
}

private int bfs(ArrayList<Zuma> curr, int k, HashSet<Long> visited) {
if (curr.isEmpty()) {
return -1;
}

final ArrayList<Zuma> next = new ArrayList<>();

for (Zuma zuma : curr) {
ArrayList<Zuma> neib = zuma.getNextLevel(k, visited);
if (neib == null) {
return k + 1;
}

next.addAll(neib);
}
return bfs(next, k + 1, visited);
}
}

record Zuma(long board, long hand) {
public static Zuma create(String boardStr, String handStr) {
return new Zuma(Zuma.encode(boardStr, false), Zuma.encode(handStr, true));
}

public ArrayList<Zuma> getNextLevel(int depth, HashSet<Long> visited) {
final ArrayList<Zuma> next = new ArrayList<>();
final ArrayList<long[]> handList = this.buildHandList();
final long[] boardList = new long[32];
final int size = this.buildBoardList(boardList);

for (long[] pair : handList) {
for (int i = 0; i < size; ++i) {
final long rawBoard = pruningCheck(boardList[i], pair[0], i * 3, depth);
if (rawBoard == -1) {
continue;
}

final long nextBoard = updateBoard(rawBoard);
if (nextBoard == 0) {
return null;
}

if (pair[1] == 0 || visited.contains(nextBoard)) {
continue;
}

visited.add(nextBoard);
next.add(new Zuma(nextBoard, pair[1]));
}
}
return next;
}

private long pruningCheck(long insBoard, long ball, int pos, int depth) {
final long L = (insBoard >> (pos + 3)) & 0x7;
final long R = (insBoard >> (pos - 3)) & 0x7;

if (depth == 0 && (ball != R) && (L != R) || depth > 0 && (ball != R)) {
return -1;
}
return insBoard | (ball << pos);
}

private long updateBoard(long board) {
long stack = 0;

for (int i = 0; i < 64; i += 3) {
final long curr = (board >> i) & 0x7;
final long top = (stack) &0x7;

// pop (if possible)
if ((top > 0) && (curr != top) && (stack & 0x3F) == ((stack >> 3) & 0x3F)) {
stack >>= 9;
if ((stack & 0x7) == top) stack >>= 3;
}

if (curr == 0) {
// done
break;
}
// push and continue
stack = (stack << 3) | curr;
}
return stack;
}

private ArrayList<long[]> buildHandList() {
final ArrayList<long[]> handList = new ArrayList<>();
long prevBall = 0;
long ballMask = 0;

for (int i = 0; i < 16; i += 3) {
final long currBall = (this.hand >> i) & 0x7;
if (currBall == 0) {
break;
}

if (currBall != prevBall) {
prevBall = currBall;
handList.add(
new long[] {currBall, ((this.hand >> 3) & ~ballMask) | (this.hand & ballMask)});
}
ballMask = (ballMask << 3) | 0x7;
}
return handList;
}

private int buildBoardList(long[] buffer) {
int ptr = 0;
long ballMask = 0x7;
long insBoard = this.board << 3;
buffer[ptr++] = insBoard;

while (true) {
final long currBall = this.board & ballMask;
if (currBall == 0) {
break;
}

ballMask <<= 3;
insBoard = (insBoard | currBall) & ~ballMask;
buffer[ptr++] = insBoard;
}
return ptr;
}

private static long encode(String stateStr, boolean sortFlag) {
final char[] stateChars = stateStr.toCharArray();
if (sortFlag) {
Arrays.sort(stateChars);
}

long stateBits = 0;
for (char ch : stateChars) {
stateBits = (stateBits << 3) | Zuma.encode(ch);
}
return stateBits;
}

private static long encode(char ch) {
return switch (ch) {
case 'R' -> 0x1;
case 'G' -> 0x2;
case 'B' -> 0x3;
case 'W' -> 0x4;
case 'Y' -> 0x5;
case ' ' -> 0x0;
default ->
throw new IllegalArgumentException("Invalid char: " + ch);
};
}
}
```

<!-- tabs:end -->

<!-- end -->
Loading
Loading