From 760fee5b0c39efd65c6dd78d61f6f532084234d3 Mon Sep 17 00:00:00 2001 From: harper0201 <963444707@qq.com> Date: Tue, 6 Sep 2022 01:07:58 -0400 Subject: [PATCH 1/5] add java code for flood fill --- contents/flood_fill/code/java/flood_fill.java | 71 +++++++++++++++++++ contents/flood_fill/code/java/main.java | 30 ++++++++ contents/flood_fill/flood_fill.md | 8 +++ 3 files changed, 109 insertions(+) create mode 100644 contents/flood_fill/code/java/flood_fill.java create mode 100644 contents/flood_fill/code/java/main.java diff --git a/contents/flood_fill/code/java/flood_fill.java b/contents/flood_fill/code/java/flood_fill.java new file mode 100644 index 000000000..e09de3adf --- /dev/null +++ b/contents/flood_fill/code/java/flood_fill.java @@ -0,0 +1,71 @@ +package code.java; + +import java.util.*; +class Point{ + int x; + int y; + public Point(int x,int y){ + this.x = x; + this.y = y; + } +} + +public class flood_fill { + public boolean inbound(int[][] grid, Point p){ + int row = grid.length, col = grid[0].length; + if (p.x < 0 || p.x >= row || p.y < 0 || p.y >= col) return false; + return true; + } + + public List find_neighbors(int[][] grid, Point p, int old_val, int new_val){ + //north, south, east, west neighbors + List possible_neighbors = new ArrayList<>(); + List neighbors = new ArrayList<>(); + possible_neighbors.add(new Point(p.x,p.y + 1)); + possible_neighbors.add(new Point(p.x,p.y - 1)); + possible_neighbors.add(new Point(p.x - 1, p.y)); + possible_neighbors.add(new Point(p.x + 1, p.y)); + //exclude the neighbors that go out of bounds and should not be colored + for (Point possible_neighbor : possible_neighbors){ + if(inbound(grid,possible_neighbor) && grid[possible_neighbor.x][possible_neighbor.y] == old_val){ + neighbors.add(possible_neighbor); + } + } + return neighbors; + } + + public void stack_fill(int[][] grid, Point p, int old_val, int new_val){ + if (old_val == new_val) return; + Stack stack = new Stack<>(); + stack.add(p); + while (!stack.empty()){ + Point cur = stack.pop(); + grid[cur.x][cur.y] = new_val; + for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){ + stack.add(neighbor); + } + } + } + + public void recursive_fill(int[][] grid, Point p, int old_val, int new_val){ + if (old_val == new_val) return; + grid[p.x][p.y] = new_val; + for (Point neighbor : find_neighbors(grid, p, old_val, new_val)){ + recursive_fill(grid, neighbor, old_val, new_val); + } + } + + public void queue_fill(int[][] grid, Point p, int old_val, int new_val){ + if (old_val == new_val) return; + Queue queue = new ArrayDeque<>(); + queue.add(p); + while (!queue.isEmpty()){ + Point cur = queue.remove(); + for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){ + grid[neighbor.x][neighbor.y] = new_val; + queue.add(neighbor); + } + } + } +} + diff --git a/contents/flood_fill/code/java/main.java b/contents/flood_fill/code/java/main.java new file mode 100644 index 000000000..496a82447 --- /dev/null +++ b/contents/flood_fill/code/java/main.java @@ -0,0 +1,30 @@ +package code.java; + +public class main { + public static void main(String[] args) { + flood_fill f = new flood_fill(); + Point start = new Point(0,0); + + int[][] grid = { + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}}; + + int[][] solution = { + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}}; + + f.recursive_fill(grid, start,0,1); + assert(grid.clone() == solution); + f.queue_fill(grid, start, 0, 1); + assert(grid.clone() == solution); + f.stack_fill(grid, start, 0, 1); + assert(grid.clone() == solution); + + } +} diff --git a/contents/flood_fill/flood_fill.md b/contents/flood_fill/flood_fill.md index ed84683a6..e6d40aedc 100644 --- a/contents/flood_fill/flood_fill.md +++ b/contents/flood_fill/flood_fill.md @@ -96,6 +96,8 @@ In code, this might look like this: [import:10-25, lang="python"](code/python/flood_fill.py) {% sample lang="coco" %} [import:15-20, lang="coconut"](code/coconut/flood_fill.coco) +{% sample lang="java" %} +[import:20-35, lang="java"](code/coconut/flood_fill.java) {% endmethod %} @@ -118,6 +120,8 @@ In code, it might look like this: [import:55-63, lang="python"](code/python/flood_fill.py) {% sample lang="coco" %} [import:52-61, lang:"coconut"](code/coconut/flood_fill.coco) +{% sample lang="java" %} +[import:50-56, lang="java"](code/coconut/flood_fill.java) {% endmethod %} The above code continues recursing through available neighbors as long as neighbors exist, and this should work so long as we are adding the correct set of neighbors. @@ -135,6 +139,8 @@ Additionally, it is possible to do the same type of traversal by managing a stac [import:27-36, lang="python"](code/python/flood_fill.py) {% sample lang="coco" %} [import:23-34, lang:"coconut"](code/coconut/flood_fill.coco) +{% sample lang="java" %} +[import:37-48, lang="java"](code/coconut/flood_fill.java) {% endmethod %} This is ultimately the same method of traversal as before; however, because we are managing our own data structure, there are a few distinct differences: @@ -180,6 +186,8 @@ The code would look something like this: [import:38-53, lang="python"](code/python/flood_fill.py) {% sample lang="coco" %} [import:36-49, lang:"coconut"](code/coconut/flood_fill.coco) +{% sample lang="java" %} +[import:58-69, lang="java"](code/coconut/flood_fill.java) {% endmethod %} Now, there is a small trick in this code that must be considered to make sure it runs optimally. From 3cd0c3c18710832a48981ed6da8ff898ef0ae040 Mon Sep 17 00:00:00 2001 From: harper0201 <963444707@qq.com> Date: Tue, 6 Sep 2022 01:08:51 -0400 Subject: [PATCH 2/5] add java code for flood fill --- contents/flood_fill/flood_fill.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/flood_fill/flood_fill.md b/contents/flood_fill/flood_fill.md index e6d40aedc..1c536f072 100644 --- a/contents/flood_fill/flood_fill.md +++ b/contents/flood_fill/flood_fill.md @@ -187,7 +187,7 @@ The code would look something like this: {% sample lang="coco" %} [import:36-49, lang:"coconut"](code/coconut/flood_fill.coco) {% sample lang="java" %} -[import:58-69, lang="java"](code/coconut/flood_fill.java) +[import:58-69`, lang="java"](code/coconut/flood_fill.java) {% endmethod %} Now, there is a small trick in this code that must be considered to make sure it runs optimally. From 427030d9af4417cbb83d5e1cc13941ee4aba8a0b Mon Sep 17 00:00:00 2001 From: harper0201 <963444707@qq.com> Date: Tue, 6 Sep 2022 17:50:40 -0400 Subject: [PATCH 3/5] fix the bug of fill flood implementation by java --- contents/flood_fill/code/java/flood_fill.java | 71 +------------------ contents/flood_fill/code/java/main.java | 30 -------- contents/flood_fill/flood_fill.md | 10 +-- 3 files changed, 7 insertions(+), 104 deletions(-) delete mode 100644 contents/flood_fill/code/java/main.java diff --git a/contents/flood_fill/code/java/flood_fill.java b/contents/flood_fill/code/java/flood_fill.java index e09de3adf..1655029eb 100644 --- a/contents/flood_fill/code/java/flood_fill.java +++ b/contents/flood_fill/code/java/flood_fill.java @@ -1,71 +1,2 @@ -package code.java; - -import java.util.*; -class Point{ - int x; - int y; - public Point(int x,int y){ - this.x = x; - this.y = y; - } +package code.java;public class flood_fill { } - -public class flood_fill { - public boolean inbound(int[][] grid, Point p){ - int row = grid.length, col = grid[0].length; - if (p.x < 0 || p.x >= row || p.y < 0 || p.y >= col) return false; - return true; - } - - public List find_neighbors(int[][] grid, Point p, int old_val, int new_val){ - //north, south, east, west neighbors - List possible_neighbors = new ArrayList<>(); - List neighbors = new ArrayList<>(); - possible_neighbors.add(new Point(p.x,p.y + 1)); - possible_neighbors.add(new Point(p.x,p.y - 1)); - possible_neighbors.add(new Point(p.x - 1, p.y)); - possible_neighbors.add(new Point(p.x + 1, p.y)); - //exclude the neighbors that go out of bounds and should not be colored - for (Point possible_neighbor : possible_neighbors){ - if(inbound(grid,possible_neighbor) && grid[possible_neighbor.x][possible_neighbor.y] == old_val){ - neighbors.add(possible_neighbor); - } - } - return neighbors; - } - - public void stack_fill(int[][] grid, Point p, int old_val, int new_val){ - if (old_val == new_val) return; - Stack stack = new Stack<>(); - stack.add(p); - while (!stack.empty()){ - Point cur = stack.pop(); - grid[cur.x][cur.y] = new_val; - for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){ - stack.add(neighbor); - } - } - } - - public void recursive_fill(int[][] grid, Point p, int old_val, int new_val){ - if (old_val == new_val) return; - grid[p.x][p.y] = new_val; - for (Point neighbor : find_neighbors(grid, p, old_val, new_val)){ - recursive_fill(grid, neighbor, old_val, new_val); - } - } - - public void queue_fill(int[][] grid, Point p, int old_val, int new_val){ - if (old_val == new_val) return; - Queue queue = new ArrayDeque<>(); - queue.add(p); - while (!queue.isEmpty()){ - Point cur = queue.remove(); - for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){ - grid[neighbor.x][neighbor.y] = new_val; - queue.add(neighbor); - } - } - } -} - diff --git a/contents/flood_fill/code/java/main.java b/contents/flood_fill/code/java/main.java deleted file mode 100644 index 496a82447..000000000 --- a/contents/flood_fill/code/java/main.java +++ /dev/null @@ -1,30 +0,0 @@ -package code.java; - -public class main { - public static void main(String[] args) { - flood_fill f = new flood_fill(); - Point start = new Point(0,0); - - int[][] grid = { - {0, 0, 1, 0, 0}, - {0, 0, 1, 0, 0}, - {0, 0, 1, 0, 0}, - {0, 0, 1, 0, 0}, - {0, 0, 1, 0, 0}}; - - int[][] solution = { - {1, 1, 1, 0, 0}, - {1, 1, 1, 0, 0}, - {1, 1, 1, 0, 0}, - {1, 1, 1, 0, 0}, - {1, 1, 1, 0, 0}}; - - f.recursive_fill(grid, start,0,1); - assert(grid.clone() == solution); - f.queue_fill(grid, start, 0, 1); - assert(grid.clone() == solution); - f.stack_fill(grid, start, 0, 1); - assert(grid.clone() == solution); - - } -} diff --git a/contents/flood_fill/flood_fill.md b/contents/flood_fill/flood_fill.md index 1c536f072..857d241d9 100644 --- a/contents/flood_fill/flood_fill.md +++ b/contents/flood_fill/flood_fill.md @@ -97,7 +97,7 @@ In code, this might look like this: {% sample lang="coco" %} [import:15-20, lang="coconut"](code/coconut/flood_fill.coco) {% sample lang="java" %} -[import:20-35, lang="java"](code/coconut/flood_fill.java) +[import:17-32, lang="java"](code/java/flood_fill.java) {% endmethod %} @@ -121,7 +121,7 @@ In code, it might look like this: {% sample lang="coco" %} [import:52-61, lang:"coconut"](code/coconut/flood_fill.coco) {% sample lang="java" %} -[import:50-56, lang="java"](code/coconut/flood_fill.java) +[import:45-51, lang="java"](code/java/flood_fill.java) {% endmethod %} The above code continues recursing through available neighbors as long as neighbors exist, and this should work so long as we are adding the correct set of neighbors. @@ -140,7 +140,7 @@ Additionally, it is possible to do the same type of traversal by managing a stac {% sample lang="coco" %} [import:23-34, lang:"coconut"](code/coconut/flood_fill.coco) {% sample lang="java" %} -[import:37-48, lang="java"](code/coconut/flood_fill.java) +[import:33-44, lang="java"](code/java/flood_fill.java) {% endmethod %} This is ultimately the same method of traversal as before; however, because we are managing our own data structure, there are a few distinct differences: @@ -187,7 +187,7 @@ The code would look something like this: {% sample lang="coco" %} [import:36-49, lang:"coconut"](code/coconut/flood_fill.coco) {% sample lang="java" %} -[import:58-69`, lang="java"](code/coconut/flood_fill.java) +[import:52-63, lang="java"](code/java/flood_fill.java) {% endmethod %} Now, there is a small trick in this code that must be considered to make sure it runs optimally. @@ -272,6 +272,8 @@ After, we will fill in the left-hand side of the array to be all ones by choosin [import:, lang="python"](code/python/flood_fill.py) {% sample lang="coco" %} [import, lang="coconut"](code/coconut/flood_fill.coco) +{% sample lang="java" %} +[import, lang="java"](code/java/flood_fill.java) {% endmethod %} From d37361a7164e8bc6aabf758e2313dba7ccc80e0b Mon Sep 17 00:00:00 2001 From: harper0201 <963444707@qq.com> Date: Tue, 6 Sep 2022 23:39:18 -0400 Subject: [PATCH 4/5] "refix it" --- contents/flood_fill/code/java/flood_fill.java | 97 ++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/contents/flood_fill/code/java/flood_fill.java b/contents/flood_fill/code/java/flood_fill.java index 1655029eb..2719c722f 100644 --- a/contents/flood_fill/code/java/flood_fill.java +++ b/contents/flood_fill/code/java/flood_fill.java @@ -1,2 +1,97 @@ -package code.java;public class flood_fill { +package code.java; +import java.util.*; +public class flood_fill { + static class Point{ + int x; + int y; + public Point(int x,int y){ + this.x = x; + this.y = y; + } + } + public static boolean inbound(int[][] grid, Point p){ + int row = grid.length, col = grid[0].length; + if (p.x < 0 || p.x >= row || p.y < 0 || p.y >= col) return false; + return true; + } + public static List find_neighbors(int[][] grid, Point p, int old_val, int new_val){ + //north, south, east, west neighbors + List possible_neighbors = new ArrayList<>(); + List neighbors = new ArrayList<>(); + possible_neighbors.add(new Point(p.x,p.y + 1)); + possible_neighbors.add(new Point(p.x,p.y - 1)); + possible_neighbors.add(new Point(p.x - 1, p.y)); + possible_neighbors.add(new Point(p.x + 1, p.y)); + //exclude the neighbors that go out of bounds and should not be colored + for (Point possible_neighbor : possible_neighbors){ + if(inbound(grid,possible_neighbor) && grid[possible_neighbor.x][possible_neighbor.y] == old_val){ + neighbors.add(possible_neighbor); + } + } + return neighbors; + } + public static void stack_fill(int[][] grid, Point p, int old_val, int new_val){ + if (old_val == new_val) return; + Stack stack = new Stack<>(); + stack.add(p); + while (!stack.empty()){ + Point cur = stack.pop(); + grid[cur.x][cur.y] = new_val; + for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){ + stack.add(neighbor); + } + } + } + public static void recursive_fill(int[][] grid, Point p, int old_val, int new_val){ + if (old_val == new_val) return; + grid[p.x][p.y] = new_val; + for (Point neighbor : find_neighbors(grid, p, old_val, new_val)){ + recursive_fill(grid, neighbor, old_val, new_val); + } + } + public static void queue_fill(int[][] grid, Point p, int old_val, int new_val){ + if (old_val == new_val) return; + Queue queue = new ArrayDeque<>(); + queue.add(p); + while (!queue.isEmpty()){ + Point cur = queue.remove(); + for (Point neighbor : find_neighbors(grid, cur, old_val, new_val)){ + grid[neighbor.x][neighbor.y] = new_val; + queue.add(neighbor); + } + } + } + public static boolean isEqual(int[][] grid, int[][] solution){ + for(int i = 0; i < grid.length; i++){ + for(int j = 0; j < grid[0].length; j++) { + if(grid[i][j] != solution[i][j]) return false; + } + } + return true; + } + public static void main(String[] args) { + Point start = new Point(0,0); + int[][] grid = { + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}}; + + int[][] solution = { + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}, + {1, 1, 1, 0, 0}}; + + queue_fill(grid, start, 0, 1); + if(isEqual(grid, solution)) System.out.println("test pass"); + stack_fill(grid, start, 0, 1); + if(isEqual(grid, solution)) System.out.println("test pass"); + recursive_fill(grid, start, 0, 1); + if(isEqual(grid, solution)) System.out.println("test pass"); + } } + + From 217ccc2c07864963ffbca26d3ab4dc57101b43e0 Mon Sep 17 00:00:00 2001 From: harper0201 <78258592+harper0201@users.noreply.github.com> Date: Fri, 9 Sep 2022 09:36:02 -0400 Subject: [PATCH 5/5] Update contents/flood_fill/code/java/flood_fill.java Co-authored-by: Sammy Plat --- contents/flood_fill/code/java/flood_fill.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contents/flood_fill/code/java/flood_fill.java b/contents/flood_fill/code/java/flood_fill.java index 2719c722f..187ff2bd6 100644 --- a/contents/flood_fill/code/java/flood_fill.java +++ b/contents/flood_fill/code/java/flood_fill.java @@ -11,8 +11,7 @@ public Point(int x,int y){ } public static boolean inbound(int[][] grid, Point p){ int row = grid.length, col = grid[0].length; - if (p.x < 0 || p.x >= row || p.y < 0 || p.y >= col) return false; - return true; + return (p.x < 0 || p.x >= row || p.y < 0 || p.y >= col); } public static List find_neighbors(int[][] grid, Point p, int old_val, int new_val){ //north, south, east, west neighbors