Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

My problem is that the solution to the map is not marked right

$
0
0
When I run my code for my maze it solves the maze but it doesn't print the correct path.

/**
 * @author Chris
 *
 */
public class Maze {
	// Class Variables
	final static char BLANK = ' ';
	final static char WALL = 'X';
	final static char BACK = 'B';
	final static char PATH = '-';
	static char[][] map=null;

	Maze(String [] n)
	{
		map= new char[n.length][];
		for(int i =0;i<n.length;i++){
			map[i] = n[i].toCharArray();
		}

	}

	public boolean findSolution(int row, int col)
	  {
	    boolean done = false;
	    if (valid (row, col))
	    {
	      map[row][col] = PATH; // this cell has been tried
	      if (row == map.length-1 && col == map[0].length-1)
	        done = true; // the maze is solved
	      else
	      {
	        done = findSolution (row, col+1); // east
	        if (!done)
	          done = findSolution (row-1, col); // north
	        if (!done)
	          done = findSolution (row, col-1); // west
	        if (!done)
	          done = findSolution (row+1, col); // south
	      }
	      if (done) // this location is part of the final path
	        map[row][col] = PATH;
	    }
	    return done;
	  }
	  
	  private boolean valid (int row, int column)
	  {
	    boolean result = false;
	// check if cell is in the bounds of the matrix
	    if (row >= 0 && row < map.length &&
	        column >= 0 && column < map[row].length)
	// check if cell is not blocked and not previously tried
	      if (map[row][column] == BLANK) 
	      result = true;
	    return result;
	  }

	public boolean solution()
	{
		return findSolution(4,0);
	}

	public void displayMaze()
	{
		final char[ ][ ] maze = map;
		for(int i = 0; i < maze.length; i++){
			for(int j = 0; j < maze.length; j++)
				System.out.print("|"+maze[i][j]);
			System.out.println("|");
		}
	}
}




public class Main3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Maze maze1 = new Maze( new String[]
							{"xxxxxxxxxxxx",
							 "x xxxxxxxxxx",
							 "x   xxxxxx  ",
							 "xxx x   xx x",
							 "    x x xx x",
							 "x xxx  xx  x",
							 "x  xxx xx xx",
							 "xx     x  xx",
							 "xx xxxxx xxx",
							 "xx   x   xxx",
							 "xxxx   xxxxx",
							 "xxxxxxxxxxxx"});
		
		maze1.solution();
		maze1.displayMaze();

	}

}



here is what it prints

|x|x|x|x|x|x|x|x|x|x|x|x|
|x|-|x|x|x|x|x|x|x|x|x|x|
|x|-|-|-|x|x|x|x|x|x|-|-|
|x|x|x|-|x|-|-|-|x|x|-|x|
|-|-|-|-|x|-|x|-|x|x|-|x|
|x|-|x|x|x|-|-|x|x|-|-|x|
|x|-|-|x|x|x|-|x|x|-|x|x|
|x|x|-|-|-|-|-|x|-|-|x|x|
|x|x|-|x|x|x|x|x|-|x|x|x|
|x|x|-|-|-|x|-|-|-|x|x|x|
|x|x|x|x|-|-|-|x|x|x|x|x|
|x|x|x|x|x|x|x|x|x|x|x|x|


and it needs to look like this

|x|x|x|x|x|x|x|x|x|x|x|x|
|x|B|x|x|x|x|x|x|x|x|x|x|
|x|B|B|B|x|x|x|x|x|x|-|-|
|x|x|x|B|x|B|B|B|x|x|-|x|
|-|-|B|B|x|B|x|B|x|x|-|x|
|x|-|x|x|x|B|B|x|x|-|-|x|
|x|-|-|x|x|x|B|x|x|-|x|x|
|x|x|-|B|B|B|B|x|-|-|x|x|
|x|x|-|x|x|x|x|x|-|x|x|x|
|x|x|-|-|-|x|-|-|-|x|x|x|
|x|x|x|x|-|-|-|x|x|x|x|x|
|x|x|x|x|x|x|x|x|x|x|x|x|

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>