From c63c278f3d4338fb63ef6c9534b4d8d99e9d82d0 Mon Sep 17 00:00:00 2001 From: alis-lc Date: Thu, 26 Oct 2023 11:01:18 +0000 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=9B=9E=E6=BA=AF=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/alis/smallcc/algorithm/FloodFill.java | 67 +++++++++ .../org/alis/smallcc/algorithm/Labyrinth.java | 136 ++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 datastructure/src/main/java/org/alis/smallcc/algorithm/FloodFill.java create mode 100644 datastructure/src/main/java/org/alis/smallcc/algorithm/Labyrinth.java diff --git a/datastructure/src/main/java/org/alis/smallcc/algorithm/FloodFill.java b/datastructure/src/main/java/org/alis/smallcc/algorithm/FloodFill.java new file mode 100644 index 0000000..3634675 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/algorithm/FloodFill.java @@ -0,0 +1,67 @@ +package org.alis.smallcc.algorithm; +/** + * 回溯 + * 洪水填充算法 + * - 题例: 洪水会在一瞬间向四周填充 + * - 从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色, + * 直到封闭区域内的所有节点都被处理过为止, + * 是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法。 + * + * @author lc + * @date 2023/10/26 17:18 + */ +public class FloodFill { + + private final int[][] table = new int[][]{ + {1, 0, 0, 1}, + {1, 0, 1, 1}, + {0, 0, 0, 1}, + {0, 1, 1, 1}, + }; + + public static void main(String[] args) { + new FloodFill().go(); + } + public void go() { + // 首先需要知道洪水迸发的点 + recursion(0, 1); + for (int[] ints : table) { + for (int anInt : ints) { + System.out.print(anInt + " "); + } + System.out.println(); + } + } + + + private void recursion(int x, int y) { + boolean flag = table[x][y] == 0; + // 开始填充 + if (flag) { + table[x][y] = 3; + } else { + return; + } + // 开始想四周扩散 + // 向上扩散 + if (x - 1 >= 0) { + recursion(x - 1, y); + } + // 向下扩散 + if (x + 1 < table.length) { + recursion(x + 1, y); + } + + + // 向左扩散 + if (y - 1 >= 0) { + recursion(x, y - 1); + } + + // 向右扩散 + if (y + 1 < table[0].length) { + recursion(x, y + 1); + } + + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/algorithm/Labyrinth.java b/datastructure/src/main/java/org/alis/smallcc/algorithm/Labyrinth.java new file mode 100644 index 0000000..870c7d1 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/algorithm/Labyrinth.java @@ -0,0 +1,136 @@ +package org.alis.smallcc.algorithm; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +/** + * 回溯 + * 洪水填充算法 + * - 题例: 洪水会在一瞬间向四周填充 + * - 从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色, + * 直到封闭区域内的所有节点都被处理过为止, + * 是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法。 + * + * @author lc + * @date 2023/10/26 17:18 + */ +public class Labyrinth { + + + + + private final int[][] labyrinth = new int[][]{ + {0, 1, 0, 0}, + {0, 0, 0, 1}, + {0, 1, 0, 1}, + {0, 0, 0, 0} + }; + + public static void main(String[] args) { + new Labyrinth().goLabyrinth(); + } + + + + + /** + * 走迷宫 + */ + public void goLabyrinth() { + // 迷宫有起点和终点 + int start_x = 0; + int start_y = 0; + + int end_x = 3; + int end_y = 3; + + Set set = new HashSet<>(); + go(new Node(end_x, end_y), new Node(start_x, start_y), set); + + + } + + private void go(Node end, Node current, Set set) { + // 如果是终点就直接返还 + if (current.equals(end)) { + set.add(current); + print(set); + return; + } + // 此路不同 + if (labyrinth[current.getX()][current.getY()] == 1) { + return; + } + // 说明已经走过了 + if (set.contains(current)) { + return; + } + set.add(current); + + + // 往下 + if (current.getX() + 1 < labyrinth.length) { + go(end, new Node(current.getX() + 1, current.getY()), set); + } + // 往下 + if (current.getX() - 1 >= 0) { + go(end, new Node(current.getX() - 1, current.getY()), set); + } + // 往下 + if (current.getY() + 1 < labyrinth[0].length) { + go(end, new Node(current.getX(), current.getY() + 1), set); + } + // 往下 + if (current.getY() - 1 >= 0) { + go(end, new Node(current.getX(), current.getY() - 1), set); + } + + set.remove(current); + } + + private void print(Set set) { + for (int i = 0; i < labyrinth.length; i++) { + for (int j = 0; j < labyrinth[i].length; j++) { + if (set.contains(new Node(i, j))) { + System.out.print("0"); + } else { + System.out.print(" "); + } + } + System.out.println(); + } + } + + private static class Node { + private final int x; + private final int y; + + public Node(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Node node = (Node) o; + return x == node.x && y == node.y; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } + } + +}