![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 7
Rep Power: 0
![]() |
Recursion Maze Question
In my computer science class we solved mazes of 0s and 1s using recursion
1 0 0 0 0 1 1 0 1 0 0 1 1 0 1 1 0 1 1 1 0 0 1 0 1 you can only move from a 1 to a 1 and u have to get from the top left to the bottom right. I got the code to say whether or not you can get to the end of the maze, but for extra credit we are supposed to count the number of steps to get there. I was wondering if sum1 could help get in the right direction to counting the number of steps because i dont understand how you could do it since it would count all of the 1s that are connected. Here is my code: //Julian
//Recursion Maze
//Comp Sci II AP
import java.io.*;
import java.util.*;
public class recursion
{
public static void main(String[] args) throws IOException
{
Scanner file=new Scanner(new File("recursion.txt"));
int length=file.nextInt();
int[][] maze=new int[length][length];
for(int x=0;x<length;x++)
{
for(int y=0;y<length;y++)
{
maze[x][y]=file.nextInt();
}
}
mazeSolver ms=new mazeSolver();
ms.mazeSolver(maze);ms.solving(0,0);
System.out.println(ms);
}
}
class mazeSolver
{
boolean exitFound=false;
int[][] maze;
public void mazeSolver(int[][] m)
{
maze=m;
}
public boolean solving(int r,int c)
{
if(r>=0&&c>=0&&r<maze.length&&c<maze.length&&maze[r][c]==1)
{
maze[r][c]=2;
if(r==maze.length-1&&c==maze.length-1)
{
exitFound=true;
}
else
{
solving(r+1,c);
solving(r-1,c);
solving(r,c+1);
solving(r,c-1);
}
}
return exitFound;
}
public String toString()
{
String output;
if(exitFound)
{
output="Exit Found";
}
else
{
output="Exit Not Found";
}
return output;
}
}and here is the data 5 1 0 0 0 0 1 1 0 1 0 0 1 1 0 1 1 0 1 1 1 0 0 1 0 1 |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with maze | jl_7 | C++ | 3 | Apr 18th, 2006 7:03 AM |
| Attitudes | Oddball | Coder's Corner Lounge | 29 | Mar 18th, 2006 9:34 PM |
| Recursion Question for a "gameboard" | keweedsmo | C++ | 14 | Feb 13th, 2006 2:59 PM |
| Avoiding Stackoverflow error using recursion to solve a maze | Mjordan2nd | Java | 1 | Feb 9th, 2006 10:01 PM |
| c++ recursion question | browning_man9 | C++ | 9 | Jan 26th, 2006 6:00 PM |