Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have coded differently and got a faster time.</p> <pre><code>import java.util.ArrayList; import java.util.List; public class PathFinder { public static void main(String[] args) { int[][] set1 = new int[][]{{2, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 3, 1, 1}}; long start = System.nanoTime(); Maze maze = new Maze(set1); List&lt;String&gt; paths = new ArrayList&lt;String&gt;(); maze.findPaths(0, 0, new StringBuilder(), paths); System.out.printf("Paths found %,d%n", paths.size()); long time = System.nanoTime() - start; System.out.printf("Took %,.1f seconds%n", time / 1e9); } } class Node { final int num; private boolean traversed; private final String name; Node(int num, String name) { this.num = num; this.name = name; } public boolean isTraversed() { return traversed; } public void setTraversed(boolean traversed) { this.traversed = traversed; } public String getName() { return name; } public boolean isExit() { return num == 3; } public boolean isPit() { return num == 1; } } class Maze { private final Node[][] nodes; private final int rows, cols; private final int nonPits; public Maze(int[][] matrix) { rows = matrix.length; cols = matrix[0].length; nodes = new Node[rows][cols]; int nonPits = 0; for (int i = 0; i &lt; rows; i++) for (int j = 0; j &lt; cols; j++) { nodes[i][j] = new Node(matrix[i][j], "" + (char) ('A' + i) + (char) ('0' + j)); if (!nodes[i][j].isPit()) nonPits++; } this.nonPits = nonPits; } public void findPaths(int x, int y, StringBuilder path, List&lt;String&gt; paths) { if (x &lt; 0 || x &gt;= cols) return; if (y &lt; 0 || y &gt;= rows) return; Node node = nodes[y][x]; if (node.isTraversed() || node.isPit()) return; node.setTraversed(true); int length = path.length(); path.append(node.getName()); if (node.isExit()) { if (path.length() == nonPits*2) { paths.add(path.toString()); if (paths.size() % 10000 == 0) System.out.printf("... found %,d paths%n", paths.size()); } } else { findPaths(x - 1, y, path, paths); findPaths(x + 1, y, path, paths); findPaths(x, y - 1, path, paths); findPaths(x, y + 1, path, paths); } path.setLength(length); node.setTraversed(false); } } </code></pre> <p>prints</p> <pre><code>Paths found 40,616 Took 81.1 seconds </code></pre> <hr> <p>A lot of the time is spent recording the paths. If it just counts the paths (which requires the same amount of recursion)</p> <pre><code>import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class PathFinder { public static void main(String[] args) { int[][] set1 = new int[][]{{2, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 3, 1, 1}}; long start = System.nanoTime(); Maze maze = new Maze(set1); long[] count = {0L}; maze.findPaths(0, 0, 1, count); System.out.printf("Paths found %,d%n", count[0]); long time = System.nanoTime() - start; System.out.printf("Took %,.1f seconds%n", time / 1e9); } } class Node { final int num; private boolean traversed; private final String name; Node(int num, String name) { this.num = num; this.name = name; } public boolean isTraversed() { return traversed; } public void setTraversed(boolean traversed) { this.traversed = traversed; } public String getName() { return name; } public boolean isExit() { return num == 3; } public boolean isPit() { return num == 1; } } class Maze { private final Node[][] nodes; private final int rows, cols; private final int nonPits; public Maze(int[][] matrix) { rows = matrix.length; cols = matrix[0].length; nodes = new Node[rows][cols]; int nonPits = 0; for (int i = 0; i &lt; rows; i++) for (int j = 0; j &lt; cols; j++) { nodes[i][j] = new Node(matrix[i][j], "" + (char) ('A' + i) + (char) ('0' + j)); if (!nodes[i][j].isPit()) nonPits++; } this.nonPits = nonPits; } public void findPaths(int x, int y, int depth, long[] count) { if (x &lt; 0 || x &gt;= cols) return; if (y &lt; 0 || y &gt;= rows) return; Node node = nodes[y][x]; if (node.isTraversed() || node.isPit()) return; node.setTraversed(true); if (node.isExit()) { if (depth == nonPits) { count[0]++; if (count[0] % 10000 == 0) System.out.printf("... found %,d paths%n", count[0]); } } else { findPaths(x - 1, y, depth + 1, count); findPaths(x + 1, y, depth + 1, count); findPaths(x, y - 1, depth + 1, count); findPaths(x, y + 1, depth + 1, count); } node.setTraversed(false); } } </code></pre> <p>prints</p> <pre><code>Paths found 40,616 Took 58.5 seconds </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload