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
the data should be named "recursion.txt", and the 5 gives you the dimensions of the maze