![]() |
|
![]() |
|
|
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 |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 7
Rep Power: 0
![]() |
but if i did it like that then wouldnt i get 10 instead of the correct answer, 8
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Try it and see what happens. I can't answer that myself without punching it in.
__________________
Perhaps I should have a sticky topic for all of the times I "return" to this forum instead of a new one every time. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2006
Posts: 7
Rep Power: 0
![]() |
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
In Python, you'd do something like this: python Syntax (Toggle Plain Text)
Last edited by Arevos; Dec 17th, 2006 at 9:05 AM. |
|
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
Won't that try to or two integers together if there is more than one solution to the maze?
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
>>> 0 or False or "" or 3 or 5 3 |
|
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
Thanks Arevos, I didn't know that.
|
|
|
|
![]() |
| 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 |