Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find all available maze paths?
    primarykey
    data
    text
    <p>I am trying to write a program that is given a maze and tries to find the way out. M is the entrance, E is the exit, 1s are walls, and 0s are pathways. It is supposed to find each path and put P in the path. It is supposed to find all available paths. Right now it finds part of a path.</p> <p>Here is the code:</p> <pre><code>public class Maze { private int size; private String[][] board; private int total; //# of boards private int eX; private int eY; private int mX; private int mY; public Maze( int size, String[][] board ) { this.size = size; this.board = board; total = 0; } private void find( String c ) { int x=0, y=0; for( int i = 0; i &lt; size; i++ ) { for( int j = 0; j &lt; size; j++ ) { if( board[i][j].equals(c) ) { x = i; y = j; } } } if( c.equals("M") ) { mX = x; mY = y; } else if( c.equals("E") ) { eX = x; eY = y; } } public void findPath( ) { find( "M" ); find( "E" ); findNext( mX, mY ); } public void findNext( int x, int y ) { String last = board[x][y]; if( board[x][y].equals("P") ) board[x][y] = "1"; board[x][y] = "P"; if( rightAvailability(x,y) ) { findNext(x+1, y); } else if( leftAvailability(x,y) ) { findNext(x-1, y); } else if( aboveAvailability(x,y) ) { findNext(x, y+1); } else if( belowAvailability(x,y) ) { findNext(x, y-1); } else { total++; printBoard(); } board[x][y]= last; } public boolean rightAvailability( int x, int y ) { if( x+1 &gt;= size ) return false; else if( board[x+1][y].equals("1") ) return false; else if( board[x+1][y].equals("P") ) return false; else return true; } public boolean leftAvailability( int x, int y ) { if( x-1 &lt; 0) return false; else if( board[x-1][y].equals("1") ) return false; else if( board[x-1][y].equals("P") ) return false; else return true; } public boolean aboveAvailability( int x, int y ) { if( y+1 &gt;= size ) return false; else if( board[x][y+1].equals("1") ) return false; else if( board[x][y+1].equals("P") ) return false; else return true; } public boolean belowAvailability( int x, int y ) { if( y-1 &lt; 0) return false; else if( board[x][y-1].equals("1") ) return false; else if( board[x][y-1].equals("P") ) return false; else return true; } public void printBoard() { System.out.println( "The board number " +total+ " is: "); for(int i=0; i&lt; size; i++ ) { for(int j=0; j&lt; size; j++ ) { if( (i==mX) &amp;&amp; (j==mY) ) { System.out.print("M"); } else if( (i==eX) &amp;&amp; (j==eY) ) { System.out.print("E"); } else if( board[i][j].equals("1") ) { System.out.print("1"); } else if( board[i][j].equals("0") ) { System.out.print("0"); } else { System.out.print("P"); } } System.out.println(); } } } </code></pre> <p>Here is the tester:</p> <pre><code>public class MazeTester { public static void main(String[] args) { int size = 11; String[][] board = new String[][] { {"1","1","1","1","1","1","1","1","1","1","1"}, {"1","0","0","0","0","0","1","0","0","0","1"}, {"1","0","1","0","0","0","1","0","1","0","1"}, {"E","0","1","0","0","0","0","0","1","0","1"}, {"1","0","1","1","1","1","1","0","1","0","1"}, {"1","0","1","0","1","0","0","0","1","0","1"}, {"1","0","0","0","1","0","1","0","0","0","1"}, {"1","1","1","1","1","0","1","0","0","0","1"}, {"1","0","1","M","1","0","1","0","0","0","1"}, {"1","0","0","0","0","0","1","0","0","0","1"}, {"1","1","1","1","1","1","1","1","1","1","1"}, }; Maze m = new Maze( size, board ); m.findPath(); } } </code></pre> <p>Here is the current output:</p> <pre><code>The board number 1 is: 11111111111 1PPPPP1PPP1 1P1PPP1P1P1 EP1PPPPP1P1 101111101P1 10101PPP1P1 10001P1PPP1 11111P1PP01 101M1P1PP01 100PPP1PP01 11111111111 The board number 2 is: 11111111111 1PPPPP1PPP1 1P1PPP1P1P1 EP100PPP1P1 101111101P1 10101PPP1P1 10001P1PPP1 11111P1PP01 101M1P1PP01 100PPP1PP01 11111111111 The board number 348 is: 11111111111 1PPPPP10001 1P1PPP10101 EP1PPPPP101 1011111P101 10101PPP101 10001P10001 11111P10001 101M1P10001 100PPP10001 11111111111 </code></pre> <p>EDIT: I added if( board[x][y].equals("P") ) board[x][y] = "1"; at the beginning of findIndex. I also fixed the x &lt;= 0 problem. I updated the output to what I am getting now (it is actually print 348 similar boards).</p>
    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.
 

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