From 8cef86f0eb8b46b0ed2d7c37fa216890300249f6 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 3 Nov 2021 22:22:57 +0530 Subject: java/DFS: Don't walk on visited, Add DFS solutions, change colors It didn't walk on visited grid but printed that as an iteration so it seemed like it did. --- algorithms/java/DFS.java | 7 ++++--- lib/Fornax/CLI.rakumod | 21 +++++++++++++++------ resources/solutions/DFS-32.fornax | 13 +++++++++++++ resources/solutions/DFS-33.fornax | 25 +++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 resources/solutions/DFS-32.fornax create mode 100644 resources/solutions/DFS-33.fornax diff --git a/algorithms/java/DFS.java b/algorithms/java/DFS.java index ff4567d..e0da681 100644 --- a/algorithms/java/DFS.java +++ b/algorithms/java/DFS.java @@ -24,6 +24,9 @@ public class DFS { || curx > maze.length - 1 || cury > maze[0].length - 1) continue; + if (visited[curx][cury]) + continue; + // Marker cells. if (maze[curx][cury] == '$') System.out.print("|"); @@ -43,9 +46,7 @@ public class DFS { if (maze[curx][cury] == '$') System.exit(0); - if (visited[curx][cury]) { - continue; - } else if (maze[curx][cury] == '.') { + if (maze[curx][cury] == '.') { visited[curx][cury] = true; traverse(curx, cury, maze, visited); visited[curx][cury] = false; diff --git a/lib/Fornax/CLI.rakumod b/lib/Fornax/CLI.rakumod index 110f587..bcecff2 100644 --- a/lib/Fornax/CLI.rakumod +++ b/lib/Fornax/CLI.rakumod @@ -17,7 +17,7 @@ proto MAIN(|) is export { unless so @*ARGS { put $*USAGE; exit }; {*} } multi sub MAIN( File $input, #= fornax format file (solved) Directory :$output = '/tmp/output', #= output directory (existing) - Int :$frame-rate = 1, #= frame rate + Rat() :$frame-rate = 1, #= frame rate Bool :$verbose = True, #= verbosity ) is export { my Str @lines = $input.IO.lines; @@ -35,12 +35,16 @@ multi sub MAIN( # Colors. constant %C = ( + red => "#f2b0a2", + blue => "#b5d0ff", + cyan => "#c0efff", black => "#000000", white => "#ffffff", green => "#aecf90", - cyan => "#c0efff", - red => "#f2b0a2", - pointer => "#093060" + + pointer => "#093060", + pointer-red => "#5d3026", + pointer-green => "#184034", ).map: {.key => hex2rgb(.value)}; # Every cell must be square. Get the maximum width, height and use @@ -97,8 +101,13 @@ multi sub MAIN( .rectangle: |@target; given @grid[$r][$c] -> $cell { - when $cell eq $VIS|$CUR { - .rgba: |%C, 0.64; + when $cell eq $CUR { + .rgba: |%C, 0.56; + .rgba: |%C, 0.72 if $status == Completed; + .rgba: |%C, 0.72 if $status == Blocked; + } + when $cell eq $VIS { + .rgba: |%C, 0.64; .rgba: |%C, 0.96 if $status == Completed; .rgba: |%C, 0.96 if $status == Blocked; } diff --git a/resources/solutions/DFS-32.fornax b/resources/solutions/DFS-32.fornax new file mode 100644 index 0000000..1e8b226 --- /dev/null +++ b/resources/solutions/DFS-32.fornax @@ -0,0 +1,13 @@ +rows:7 cols:8 +.#.#..#..........#..#..$...#.#.........#.##...#......... +-#.#..#.@........#..#..$...#.#.........#.##...#......... +-#.#..#.-@.......#..#..$...#.#.........#.##...#......... +-#.#..#.--@......#..#..$...#.#.........#.##...#......... +-#.#..#.---@.....#..#..$...#.#.........#.##...#......... +-#.#..#.----@....#..#..$...#.#.........#.##...#......... +!-#.#..#.-----....#..@..$...#.#.........#.##...#......... +-#.#..#.-----@...#..#..$...#.#.........#.##...#......... +-#.#..#.------@..#..#..$...#.#.........#.##...#......... +!-#.#..@.-------..#..#..$...#.#.........#.##...#......... +-#.#..#.-------@.#..#..$...#.#.........#.##...#......... +|-#.#..#.--------.#..#..@...#.#.........#.##...#......... diff --git a/resources/solutions/DFS-33.fornax b/resources/solutions/DFS-33.fornax new file mode 100644 index 0000000..d8e221c --- /dev/null +++ b/resources/solutions/DFS-33.fornax @@ -0,0 +1,25 @@ +rows:7 cols:8 +.#.#..#..........#..#..$...#.#.........#.##...#......... +!-@.#..#..........#..#..$...#.#.........#.##...#......... +-#.#..#.@........#..#..$...#.#.........#.##...#......... +-#.#..#.-.......@#..#..$...#.#.........#.##...#......... +!-#.#..#.-.......-@..#..$...#.#.........#.##...#......... +-#.#..#.-.......-#..#..$@..#.#.........#.##...#......... +-#.#..#.-.......-#..#..$-..#.#..@......#.##...#......... +-#.#..#.-.......-#..#..$-..#.#..-......#@##...#......... +-#.#..#.-.......-#..#..$-..#.#..-......#-##...#.@....... +-#.#..#.-.......-#..#..$-..#.#..-......#-##...#.-@...... +!-#.#..#.-.......-#..#..$-..#.#..-......#-@#...#.--...... +-#.#..#.-.......-#..#..$-..#.#..-......#-##...#.--@..... +-#.#..#.-.......-#..#..$-..#.#..-......#-##...#.---@.... +-#.#..#.-.......-#..#..$-..#.#..-......#-##@..#.----.... +-#.#..#.-.......-#..#..$-..#.#..-..@...#-##-..#.----.... +!-#.#..#.-.......-#..#..$-..@.#..-..-...#-##-..#.----.... +-#.#..#.-.......-#..#..$-..#.#..-..-@..#-##-..#.----.... +-#.#..#.-.......-#..#..$-..#.#..-..--@.#-##-..#.----.... +-#.#..#.-.......-#..#..$-..#.#..-..---@#-##-..#.----.... +!-#.#..#.-.......-#..#..$-..#.#..-..----#-##-..@.----.... +-#.#..#.-.......-#..#..$-..#.#@.-..----#-##-..#.----.... +-#.#..#.-.......-#..#..$-..#.#-@-..----#-##-..#.----.... +!-#.#..#.-.......-#..#..$-..#.#---..----@-##-..#.----.... +|-#.#..#.-.......-#..#..@-..#.#---..----#-##-..#.----.... -- cgit 1.4.1-2-gfad0