Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem in recursion for Solving Maze Recursively program

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in recursion for Solving Maze Recursively program

    My program is to traverse through a maze and recursively search for '$' starting at element (1,1). I don't know if i'm running the recursion properly since my program skipps that there is a solid wall '&' and tests for it anyways. My traversable path is indicated by '#'.

    This is getting extremely annoying, any help will be very appreciated.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
     
    public class Maze
    {
     
       public static char[][] slideData;
       private int ReturnCount = 0;
    	public Maze(String s)
    	{
    		try
    		{
    			File slideFile = new File(s);
    			FileReader in = new FileReader(slideFile);
    			BufferedReader readSlide = new BufferedReader(in);
    			int length = Integer.parseInt(readSlide.readLine());
    			int width = Integer.parseInt(readSlide.readLine());
    			slideData = new char[length][width];
     
    			for(int row = 0; row < length; row++){
    				for (int col = 0; col < width; col++){
    					slideData[row][col] = (char)readSlide.read();
    				}
    				readSlide.readLine();			
    				}
    			readSlide.close();
    			in.close();
    			}catch (FileNotFoundException e)
    			{
    				System.out.println("File does not exist/ could not be found.");
    				System.err.println("FileNotFoundException: " + e.getMessage());
    			}catch(IOException e){
    				System.out.println("Problem reading file.");
    					System.err.println("IOException: " + e.getMessage());	
    		}
    	}
     
     
    	public void displayColonies()
    	{
    		char[][] temp;
    		int count; 
     
    		for(int row = 0; row < slideData.length; row++)
    		{
    			for(int col = 0; col < slideData[0].length; col++)
    			{			
     
    				if(slideData[row][col] >= 0 )
    				{
    					count = getPath(row, col);
    					if(count != 0)
    					{
    					System.out.println("(" +row + "," + col + ") Cn"+ count);
    					}
    				}
    			}
    		}
    	}
     
     
     
     
    	public void displaySlide() {
     
    		char[][] temp;
    		int count = 0; 
    		if(count == 0)
    		{
    		System.out.println("Provided Slide:");
    		count++;
    		}
    		else
    			System.out.println("Ending Slide:");
     
    		for(int row = 0; row < slideData.length; row++){
    			for(int col = 0; col < slideData[0].length; col++){
    				System.out.print(slideData[row][col]);			
    			}
    			System.out.println();
    		}
     
    		System.out.println();
    	}
     
    private int getPath (int row, int col)
       {
          if (slideData[row][col] == '$')
          {
    	System.out.println("Maze found");
          return(0);
          }
          else
     
          {
          	if (slideData[row][col] == '#')
          {
             slideData[row][col] = '%';  // this cell has been tried
     
     
     
               if (slideData[row+1][col] == '#')
                {
                	slideData[row][col] = slideData[row+1][col];
                	slideData[row][col] = '%';
                	ReturnCount = ReturnCount+1;
                	ReturnCount = getPath(row +1, col);
                	System.out.println(ReturnCount);
                }
     
                if (slideData[row-1][col] == '#')
                {
                    slideData[row][col] = slideData[row-1][col];
                	slideData[row][col] = '%';
                	ReturnCount = ReturnCount+1;;
                	ReturnCount = getPath(row +1, col);
                	System.out.println(ReturnCount);
          }
     
               if (slideData[row][col-1] == '#')
                {
                	 slideData[row][col] = slideData[row][col-1];
                 	slideData[row][col] = '%';
                 	ReturnCount = ReturnCount+1;
                	ReturnCount = getPath(row +1, col);
                	System.out.println(ReturnCount);
                }
     
                if (slideData[row][col+1] == '#')
                {
                	 slideData[row][col] = slideData[row][col+1];
                 	slideData[row][col] = '%';
                 	ReturnCount = ReturnCount+1;
                	ReturnCount = getPath(row +1, col);
                	System.out.println(ReturnCount);
                }
     
          }
          }
     
          return ReturnCount ;
       }
     
     
     
       public static void main (String[] args)
       {
    		Maze culture = new Maze("Maze.txt");
    		culture.displaySlide();
    		culture.displayColonies();
    		culture.displaySlide();
     
       }
    }


    the Maze.txt slide..
    7
    9
    &&&&&&&&&
    &#&&&&&&&
    &######&&
    &#&&#&&&&
    &#&&$&&&&
    &#######&
    &&&&&&&&&


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Solving Maze Recursively

    I've never done any map traversal before, so I thought i'd start from scratch and write my a version. It prints out the location of the finish at the end in terms of 0 based indexing for the array
    Hopefully you will be able to get an idea of where you are going wrong form this
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
     
    public class Map {
     
    	private int finishx, finishy;
    	private boolean found = false;
    	private Scanner mazeFile;
    	private char[][] maze;
     
    	private void loadMap(){
    		maze = new char[Integer.parseInt(mazeFile.nextLine())][Integer.parseInt(mazeFile.nextLine())];
    		for(int i = 0; i < maze.length; i++)
    			maze[i] = mazeFile.nextLine().toCharArray();
    	}
     
    	private void traverseMap(int y, int x, char Finish){
    		if(maze[y][x] != '#'){
    			finishx = finishy = -1;
    			return;
    		}
    		if(x != 0){
    			if(maze[y][x-1] == '#' || maze[y][x-1] == '$'){
    				if(maze[y][x-1] == '$'){
    					finishy = y;
    					finishx = x-1;
    					found = true;
    					maze[y][x] = '%';
    					return;
    				}else{
    					maze[y][x] = '%';
    					traverseMap(y, x-1, Finish);
    				}
    			}
    		}
    		if(found) return;
     
    		if(y != 0){
    			if(maze[y-1][x] == '#' || maze[y-1][x] == '$'){
    				if(maze[y-1][x] == '$'){
    					finishy = y-1;
    					finishx = x;
    					found = true;
    					maze[y][x] = '%';
    					return;
    				}else{
    					maze[y][x] = '%';
    					traverseMap(y-1, x, Finish);
    				}
    			}
    		}
    		if(found) return;
     
    		if(x != maze.length-1){
    			if(maze[y][x+1] == '#' || maze[y][x+1] == '$'){
    				if(maze[y][x+1] == '$'){
    					finishy = y;
    					finishx = x+1;
    					found = true;
    					maze[y][x] = '%';
    					return;
    				}else{
    					maze[y][x] = '%';
    					traverseMap(y, x+1, Finish);
    				}
    			}
    		}
    		if(found) return;
     
    		if(y != maze[0].length-1){
    			if(maze[y+1][x] == '#' || maze[y+1][x] == '$'){
    				if(maze[y+1][x] == '$'){
    					finishy = y+1;
    					finishx = x;
    					found = true;
    					maze[y][x] = '%';
    					return;
    				}else{
    					maze[y][x] = '%';
    					traverseMap(y+1, x, Finish);
    				}
    			}
    		}
    		if(found) return;
    	}
     
    	public Map(String File){
    		try{
    			mazeFile = new Scanner(new FileReader(File));
    		}catch(FileNotFoundException fnfe){ System.err.println(fnfe.getMessage()); }
     
    		loadMap();
    		traverseMap(1, 1, '$');
     
    		System.out.println("Value at maze["+finishy+"]["+finishx+"]: "+maze[finishy][finishx]);
    	}
     
    	public static void main(String[] args) {
    		new Map("Maze.txt");
    	}
    }
    Regards,
    Chris
    Last edited by Freaky Chris; April 29th, 2009 at 04:38 AM. Reason: change "regards" to "Regards"

Similar Threads

  1. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM