Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
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
Old Dec 4th, 2006, 5:44 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by afroboy0731 View Post
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.
If I'm understanding you correctly, it's easier than you might imagine. Imagine your recursive function like a tree, starting from a root and spreading out it's branches, with each branching point representing a step in the maze. Then the number of branching points for a particular branch would also give you the number of steps taken in the maze, would it not?
Arevos is offline   Reply With Quote
Old Dec 4th, 2006, 7:51 PM   #3
afroboy0731
Newbie
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 afroboy0731 is on a distinguished road
but if i did it like that then wouldnt i get 10 instead of the correct answer, 8
afroboy0731 is offline   Reply With Quote
Old Dec 4th, 2006, 8:29 PM   #4
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
Quote:
Originally Posted by afroboy0731 View Post
but if i did it like that then wouldnt i get 10 instead of the correct answer, 8
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.
Indigno is offline   Reply With Quote
Old Dec 16th, 2006, 6:48 PM   #5
kyndig
Newbie
 
Join Date: Jan 2006
Posts: 7
Rep Power: 0 kyndig is on a distinguished road
Quote:
Originally Posted by afroboy0731 View Post
but if i did it like that then wouldnt i get 10 instead of the correct answer, 8
I see what you are saying, you could possibly, add some code to check another step ahead, and see if that 'branch' is a dead end, if it is then dont add that as a step.
kyndig is offline   Reply With Quote
Old Dec 17th, 2006, 8:18 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by kyndig View Post
I see what you are saying, you could possibly, add some code to check another step ahead, and see if that 'branch' is a dead end, if it is then dont add that as a step.
No need. Seeing how old this post is, I don't suppose it would hurt for me to give a more exact answer to the problem. Essentially, with each recursive step you'd pass in a variable that would record the "depth" of that step. So instead of "solving(r+1,c)" you'd use, "solving(r+1,c,depth+1)". Then, when you find the exit, just return the depth. If the exit isn't found, return 0 or -1.

In Python, you'd do something like this:
python Syntax (Toggle Plain Text)
  1. def load_maze(filename):
  2. file = open(filename)
  3. length = file.readline()
  4. maze = [[int(x) for x in line.split()] for line in file]
  5. file.close()
  6. return maze
  7.  
  8. def solve_maze(maze):
  9. def explore(x, y, depth):
  10. if len(maze) == (y + 1) and len(maze[y]) == (x + 1):
  11. return depth
  12.  
  13. try: value = maze[y][x]
  14. except IndexError: return 0
  15.  
  16. if value == 0: return 0
  17.  
  18. return explore(x + 1, y, depth + 1) or
  19. explore(x - 1, y, depth + 1) or
  20. explore(x, y + 1, depth + 1) or
  21. explore(x, y - 1, depth + 1)
  22.  
  23. return explore(0, 0, 1)
  24.  
  25. solution = solve_maze(load_maze("recursion.txt"))
  26.  
  27. if solution:
  28. print "End of maze reached in %d steps" % solution
  29. else:
  30. print "No solution"

Last edited by Arevos; Dec 17th, 2006 at 9:05 AM.
Arevos is offline   Reply With Quote
Old Dec 17th, 2006, 5:07 PM   #7
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
Won't that try to or two integers together if there is more than one solution to the maze?
The Dark is offline   Reply With Quote
Old Dec 18th, 2006, 3:33 AM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by The Dark View Post
Won't that try to or two integers together if there is more than one solution to the maze?
Yes, but in Python a boolean "or" means returning the first object considered "True". For instance:
>>> 0 or False or "" or 3 or 5
3
The code doesn't find the fastest route, but does return the number of steps in the first route it finds.
Arevos is offline   Reply With Quote
Old Dec 18th, 2006, 5:18 AM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
Thanks Arevos, I didn't know that.
The Dark is offline   Reply With Quote
Reply

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 4:41 AM.

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