diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-02-03 13:43:41 -0500 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-02-19 13:50:54 -0500 |
commit | 2fa902328a4a4a76f5ae300db7810530183b47d2 (patch) | |
tree | 346ad8d60def3a6abc79a0e70994d191eb1417a8 /src/main/java/de/hysky/skyblocker | |
parent | b3132a89b5036e27d7efbb5dbe47f96e520bf851 (diff) | |
download | Skyblocker-2fa902328a4a4a76f5ae300db7810530183b47d2.tar.gz Skyblocker-2fa902328a4a4a76f5ae300db7810530183b47d2.tar.bz2 Skyblocker-2fa902328a4a4a76f5ae300db7810530183b47d2.zip |
Change IceFill from bfs to dfs
Diffstat (limited to 'src/main/java/de/hysky/skyblocker')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/dungeon/puzzle/IceFill.java | 137 |
1 files changed, 109 insertions, 28 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/puzzle/IceFill.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/puzzle/IceFill.java index 7ab96d78..166047b2 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/puzzle/IceFill.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/puzzle/IceFill.java @@ -120,50 +120,131 @@ public class IceFill extends DungeonPuzzle { return boardChanged; } - private void solve(boolean[][] iceFillBoard, List<Vector2ic> iceFillPath) { + void solve(boolean[][] iceFillBoard, List<Vector2ic> iceFillPath) { Vector2ic start = new Vector2i(iceFillBoard.length - 1, iceFillBoard[0].length / 2); int count = iceFillBoard.length * iceFillBoard[0].length - Arrays.stream(iceFillBoard).mapToInt(Booleans::countTrue).sum(); - Queue<List<Vector2ic>> queue = new ArrayDeque<>(); - queue.add(List.of(start)); - while (!queue.isEmpty()) { - List<Vector2ic> path = queue.poll(); - Vector2ic pos = path.get(path.size() - 1); - if (pos.x() == 0 && pos.y() == iceFillBoard[0].length / 2 && path.size() == count) { - iceFillPath.clear(); - iceFillPath.addAll(path); - return; - } + List<Vector2ic> newPath = solveDfs(iceFillBoard, count - 1, new ArrayList<>(List.of(start))); + if (newPath != null) { + iceFillPath.clear(); + iceFillPath.addAll(newPath); + } + } - Vector2i posMutable = pos.add(1, 0, new Vector2i()); - if (posMutable.x() < iceFillBoard.length && !iceFillBoard[posMutable.x()][posMutable.y()]) { - addQueue(queue, path, posMutable); + private List<Vector2ic> solveDfs(boolean[][] iceFillBoard, int count, List<Vector2ic> path) { + Vector2ic pos = path.get(path.size() - 1); + if (pos.x() == 0 && pos.y() == iceFillBoard[0].length / 2 && count == 0) { + return path; + } + + Vector2ic newPos = pos.add(1, 0, new Vector2i()); + if (newPos.x() < iceFillBoard.length && !iceFillBoard[newPos.x()][newPos.y()] && !path.contains(newPos)) { + path.add(newPos); + List<Vector2ic> newPath = solveDfs(iceFillBoard, count - 1, path); + if (newPath != null) { + return newPath; + } else { + path.remove(path.size() - 1); } + } - posMutable = pos.add(-1, 0, new Vector2i()); - if (posMutable.x() >= 0 && !iceFillBoard[posMutable.x()][posMutable.y()]) { - addQueue(queue, path, posMutable); + newPos = pos.add(-1, 0, new Vector2i()); + if (newPos.x() >= 0 && !iceFillBoard[newPos.x()][newPos.y()] && !path.contains(newPos)) { + path.add(newPos); + List<Vector2ic> newPath = solveDfs(iceFillBoard, count - 1, path); + if (newPath != null) { + return newPath; + } else { + path.remove(path.size() - 1); } + } - posMutable = pos.add(0, 1, new Vector2i()); - if (posMutable.y() < iceFillBoard[0].length && !iceFillBoard[posMutable.x()][posMutable.y()]) { - addQueue(queue, path, posMutable); + newPos = pos.add(0, 1, new Vector2i()); + if (newPos.y() < iceFillBoard[0].length && !iceFillBoard[newPos.x()][newPos.y()] && !path.contains(newPos)) { + path.add(newPos); + List<Vector2ic> newPath = solveDfs(iceFillBoard, count - 1, path); + if (newPath != null) { + return newPath; + } else { + path.remove(path.size() - 1); } + } - posMutable = pos.add(0, -1, new Vector2i()); - if (posMutable.y() >= 0 && !iceFillBoard[posMutable.x()][posMutable.y()]) { - addQueue(queue, path, posMutable); + newPos = pos.add(0, -1, new Vector2i()); + if (newPos.y() >= 0 && !iceFillBoard[newPos.x()][newPos.y()] && !path.contains(newPos)) { + path.add(newPos); + List<Vector2ic> newPath = solveDfs(iceFillBoard, count - 1, path); + if (newPath != null) { + return newPath; + } else { + path.remove(path.size() - 1); } } + + return null; + } + + /* + void solve(boolean[][] iceFillBoard, List<Vector2ic> iceFillPath) { + Vector2ic start = new Vector2i(iceFillBoard.length - 1, iceFillBoard[0].length / 2); + int count = iceFillBoard.length * iceFillBoard[0].length - Arrays.stream(iceFillBoard).mapToInt(Booleans::countTrue).sum(); + + Vector2ic[] newPath = solveDfs(iceFillBoard, count - 1, new Vector2ic[]{start}); + if (newPath != null) { + iceFillPath.clear(); + iceFillPath.addAll(Arrays.asList(newPath)); + } } - private void addQueue(Queue<List<Vector2ic>> queue, List<Vector2ic> path, Vector2ic newPos) { - if (!path.contains(newPos)) { - List<Vector2ic> newPath = new ArrayList<>(path); - newPath.add(newPos); - queue.add(newPath); + private Vector2ic[] solveDfs(boolean[][] iceFillBoard, int count, Vector2ic[] path) { + Vector2ic pos = path[path.length - 1]; + if (pos.x() == 0 && pos.y() == iceFillBoard[0].length / 2 && count == 0) { + return path; + } + + Vector2ic newPos = pos.add(1, 0, new Vector2i()); + if (newPos.x() < iceFillBoard.length && !iceFillBoard[newPos.x()][newPos.y()] && !ArrayUtils.contains(path, newPos)) { + Vector2ic[] newPath = Arrays.copyOf(path, path.length + 1); + newPath[path.length] = newPos; + newPath = solveDfs(iceFillBoard, count - 1, newPath); + if (newPath != null) { + return newPath; + } + } + + newPos = pos.add(-1, 0, new Vector2i()); + if (newPos.x() >= 0 && !iceFillBoard[newPos.x()][newPos.y()] && !ArrayUtils.contains(path, newPos)) { + Vector2ic[] newPath = Arrays.copyOf(path, path.length + 1); + newPath[path.length] = newPos; + newPath = solveDfs(iceFillBoard, count - 1, newPath); + if (newPath != null) { + return newPath; + } + } + + newPos = pos.add(0, 1, new Vector2i()); + if (newPos.y() < iceFillBoard[0].length && !iceFillBoard[newPos.x()][newPos.y()] && !ArrayUtils.contains(path, newPos)) { + Vector2ic[] newPath = Arrays.copyOf(path, path.length + 1); + newPath[path.length] = newPos; + newPath = solveDfs(iceFillBoard, count - 1, newPath); + if (newPath != null) { + return newPath; + } } + + newPos = pos.add(0, -1, new Vector2i()); + if (newPos.y() >= 0 && !iceFillBoard[newPos.x()][newPos.y()] && !ArrayUtils.contains(path, newPos)) { + Vector2ic[] newPath = Arrays.copyOf(path, path.length + 1); + newPath[path.length] = newPos; + newPath = solveDfs(iceFillBoard, count - 1, newPath); + if (newPath != null) { + return newPath; + } + } + + return null; } + */ @Override public void render(WorldRenderContext context) { |