473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Tips anyone

Ok, I have to solve the "Rat Maze" problem"

I am opening a file called maze.txt which looks similar to this

20 *this is # of rows
20 *this is # of columns
9 *starting row
9 *starting column
11111111111111101111
10101010101010101011
10000010001000101001
10111000100010000011
10111111111111111011
etc....

I have to find the path out of the maze using recursion

This is what I have come up with for the recursion function, ant tips?

int maze(int data[][])
{
data[r][c];

if( data[r][0] | data[0][c] | data[r][19] | data[19][c] == 0) //
base case
{
cout << "The Rat Is Free!" << endl;
}
if( data[r+1][c] == 0 )
{
cout << "Move Up" << endl;
return maze(data[r+1][c]);
}

if ( data[r-1][c] == 0 )
{
cout << "Move Down" << endl;
return maze(data[r-1][c]);
}

if ( data[r][c+1] == 0 )
{
cout << "Move Right" << endl;
return maze(data[r][c+1]);
}

if ( data[r][c-1] == 0 )
{
cout << "Move Left" << endl;
return maze(data[r][c-1]);
}

else
{
cout << "Rat your are stuck" << endl;
}
}
Feb 29 '08 #1
4 1691
On Feb 29, 2:17 pm, pleatofthepants <aaronWbry...@gmail.comwrote:
Ok, I have to solve the "Rat Maze" problem"

I am opening a file called maze.txt which looks similar to this

20 *this is # of rows
20 *this is # of columns
9 *starting row
9 *starting column
11111111111111101111
10101010101010101011
10000010001000101001
10111000100010000011
10111111111111111011
etc....

I have to find the path out of the maze using recursion

This is what I have come up with for the recursion function, ant tips?

int maze(int data[][])
{
data[r][c];

if( data[r][0] | data[0][c] | data[r][19] | data[19][c] == 0) //
base case
{
cout << "The Rat Is Free!" << endl;
}
if( data[r+1][c] == 0 )
{
cout << "Move Up" << endl;
return maze(data[r+1][c]);
}

if ( data[r-1][c] == 0 )
{
cout << "Move Down" << endl;
return maze(data[r-1][c]);
}

if ( data[r][c+1] == 0 )
{
cout << "Move Right" << endl;
return maze(data[r][c+1]);
}

if ( data[r][c-1] == 0 )
{
cout << "Move Left" << endl;
return maze(data[r][c-1]);
}

else
{
cout << "Rat your are stuck" << endl;
}

}
The way it is currently written, it looks like you get some confusing
output! For example, I can see that it is possible for your program's
output to be:

The Rat Is Free!
Rat your are stuck

Or even a combination of "The Rat Is Free" and one of the other
movement messages (and make further unnecessary moves). Also, if your
rat finds a dead end, how does your algorithm prevent going backwards
and towards the start? I don't see how you keep track of which
direction the rat came from, so it looks like your algorithm will
retry going in the direction that you came from.

On a large maze, it also looks like there is going to a heck of a lot
of recursive calls. Perhaps you can move forward using a while loop
and use recursive call to change or try different directions?
Feb 29 '08 #2
On 29 Feb, 11:19, "Jim Langston" <tazmas...@rocketmail.comwrote:
I don't want to give too much because this is info, but the function you
have shown is not recursive at all. *A recursive function calls itself.
His function is recursive, look at the points of exit.

DP
Feb 29 '08 #3
pleatofthepants wrote:
Ok, I have to solve the "Rat Maze" problem"

I am opening a file called maze.txt which looks similar to this

20 *this is # of rows
20 *this is # of columns
9 *starting row
9 *starting column
11111111111111101111
10101010101010101011
10000010001000101001
10111000100010000011
10111111111111111011
etc....

I have to find the path out of the maze using recursion

This is what I have come up with for the recursion function, ant tips?

int maze(int data[][])
This line should not compile.
{
data[r][c];
this line is bad.
>
if( data[r][0] | data[0][c] | data[r][19] | data[19][c] == 0) //
base case
{
cout << "The Rat Is Free!" << endl;
}
if( data[r+1][c] == 0 )
{
cout << "Move Up" << endl;
return maze(data[r+1][c]);
}

if ( data[r-1][c] == 0 )
{
cout << "Move Down" << endl;
return maze(data[r-1][c]);
}

if ( data[r][c+1] == 0 )
{
cout << "Move Right" << endl;
return maze(data[r][c+1]);
}

if ( data[r][c-1] == 0 )
{
cout << "Move Left" << endl;
return maze(data[r][c-1]);
}

else
{
cout << "Rat your are stuck" << endl;
}
}
Feb 29 '08 #4
Triple-DES wrote:
On 29 Feb, 11:19, "Jim Langston" <tazmas...@rocketmail.comwrote:
>I don't want to give too much because this is info, but the function
you have shown is not recursive at all. A recursive function calls
itself.

His function is recursive, look at the points of exit.
yes, you are right. I missed that. It's just not effective.

--
Jim Langston
ta*******@rocketmail.com
Mar 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel Bickett | last post by:
Hi, The truth is I despise windows xp balloon tips. Well, not so much the balloon tips, but how they're used by the operating system: like I need my system tray to tell me when there are "unused...
1
by: Roger | last post by:
I want to use and Active X DLL in Python applications. I have the win32all stuff installed and the "Excel.Application" example in the Client QuickStart section of the manual works OK, so I guess it...
34
by: Steve Horrillo | last post by:
Anyone know where to get a Tool Tips javascript that will pop up a little box when hovered for words that needs more explanation. I'm using Front Page BTW. -- Warmest regards, Steve...
1
by: Salad | last post by:
Win XP. A97. Anyone know why my tool tips don't display when the mouse is held over the field? If I reboot the computer the tool tips will display. A couple of days will pass and I'll check...
2
by: Panchi51 | last post by:
Hi, Below is a collection of tips/tricks/caveats for LP64 c coding, full text is at http://www.cs.albany.edu/~mosh/Text/c-ref.txt. Hope it helps, corrections welkome. --...
2
by: alan | last post by:
Hi all, I have create a simple function and make a pure C dll. When I use this DLL from my main program, I found I cannot see this function's definition tips (yellow box). It works fine, but no...
28
by: Yuri CHUANG | last post by:
"No newline at the end of your output" is really a problem? I've never learned that before. So I desire to know some tips about writting a program perfectly.Can anyone give me some tips which are...
7
by: Cheryl Langdon | last post by:
Does anyone know if there is a way to globally turn off ALL control tips in Access 2003 using VBA code? Thanks. --- CL
14
by: Rex | last post by:
Re: Looking for Tips/Writeup on overall approach to Exception Processing Hi All - I am fairly new to C# and am wondering how to best implement (overall) Exception Processing within my...
0
by: zzzmail.01 | last post by:
I find some useful tips about C/C++ and want to share with you. Sorry if it bothers you. Tips for better Coding Style:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.