Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Dec 4th, 2006, 4:56 PM   #1
afroboy0731
Newbie
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 afroboy0731 is on a distinguished road
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
the data should be named "recursion.txt", and the 5 gives you the dimensions of the maze
afroboy0731 is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC