Problem in recursion for Solving Maze Recursively program

• April 28th, 2009, 10:54 PM
_Coder1985
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.

Code :

```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..
Code :

```7 9 &&&&&&&&& &#&&&&&&& &######&& &#&&#&&&& &#&&\$&&&& &#######& &&&&&&&&&```
• April 29th, 2009, 04:37 AM
Freaky Chris
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 :P
Code :

```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