ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello there , I am trying to solve a problem in pascal and i have some questions to make...please help.
The problem: I have to program a man to find the way out from the garden (maze).Also the man have to find all the exits from the garden.
I used some code from a book that i have.
the pascal code:
Code:
program find;
const maxline=12;
maxcol=12;
posiblePath=' '; {road}
pathToExit='|'; {the man}
type typeMaze=array[1..maxline,1..maxcol] of char;
Var maze:typeMaze;
procedure MazeDescription (Var maze:typeMaze); {this is the garden}
Var i,j:integer;
Begin
for i:=1 to maxline do
for j:=1 to maxcol do
maze[i,j]:='#'; {this is ...suppose a flower}
maze[1,4]:=' '; maze[2,4]:=' '; maze[2,5]:=' '; maze[2,6]:=' '; {this is the path}
maze[2,7]:=' '; maze[3,4]:=' '; maze[3,7]:=' '; maze[3,8]:=' ';
maze[4,4]:=' '; maze[4,5]:=' '; maze[4,6]:=' '; maze[4,8]:=' ';
maze[4,9]:=' '; maze[5,6]:=' '; maze[5,9]:=' '; maze[6,6]:=' ';
maze[6,9]:=' '; maze[7,6]:=' '; maze[8,6]:=' '; maze[8,7]:=' ';
maze[8,8]:=' '; maze[9,5]:=' '; maze[9,6]:=' '; maze[9,8]:=' ';
maze[10,2]:=' '; maze[10,3]:=' '; maze[10,4]:=' '; maze[10,7]:=' ';
maze[10,8]:=' '; maze[11,2]:=' '; maze[11,4]:=' '; maze[11,7]:=' ';
maze[11,6]:=' '; maze[11,7]:=' '; maze[12,2]:=' ';
End;
procedure printMaze(maze:typeMaze); {printing the garden}
Var i,j:integer;
Begin
for i:=1 to maxline do
begin
for j:=1 to maxcol do
write(maze[i,j]);
writeln;
end;
writeln;
end;
function ExitFound(line,col:integer):boolean; {I dont know what this function checks}
begin {what IN means ?}
ExitFound:=(line IN [1,maxline]) or (col IN [1,maxcol]);
end;
procedure find1(maze:typeMaze; line,col:integer); {the recursive procedure to find the exit}
begin {why it find only one exit? }
maze[line,col]:=pathToExit;
if ExitFound(line,col)
then begin
printMaze(maze);
readln;
end
else begin
if maze[line-1,col]=posiblePath
then find1(maze,line-1,col);
if maze[line,col+1]=posiblePath
then find1(maze,line,col+1);
if maze[line+1,col]=posiblePath
then find1(maze,line+1,col);
if maze[line,col-1]=posiblePath
then find1(maze,line,col-1);
end;
end;
Begin
MazeDescription(maze);
find1(maze,6,6); {we start at line6 column6}
end.
Well my questions are:
(1) what does this do < ExitFound:=(line IN [1,maxline]) or (col IN [1,maxcol]); >
what does it checks ?? what it means ?
(2)why the <find1> recursive procedure does not finds all the ways out, all the exits from the garden ??
The output:
-------------------------------------------------------------------------------------------------
###|######## <--- The exit, end of the program
###| #####
###|## ####
###|||# ###
#####|## ###
#####|## ### <--- We start at (6,6)
##### ######
##### ####
#### # ####
# ## ####
# # # #####
# ##########
------------------------------------------------------------------------------------------------------------
Thx in advance !
It's an unknown Pascal dialect for me, but the code is not that hard.
1. It checks if current line is first or last, or if the current column is first of last. All such cases mean exit.
2. It can't go back. It only searches for free ways ' ', not for the ones it has on path '|'.
thx a lot Mara,
One last question, the below recursiv procedure will find all free ways ?
I think it should find all free ways...because i check recursivly to all directions..up,right,down,left.
Code:
if maze[line-1,col]=posiblePath
then find1(maze,line-1,col);
if maze[line,col+1]=posiblePath
then find1(maze,line,col+1);
if maze[line+1,col]=posiblePath
then find1(maze,line+1,col);
if maze[line,col-1]=posiblePath
then find1(maze,line,col-1);
thx a lot Mara,
One last question, the below recursiv procedure will find all free ways ?
I think it should find all free ways...because i check recursivly to all directions..up,right,down,left.
Code:
if maze[line-1,col]=posiblePath
then find1(maze,line-1,col);
if maze[line,col+1]=posiblePath
then find1(maze,line,col+1);
if maze[line+1,col]=posiblePath
then find1(maze,line+1,col);
if maze[line,col-1]=posiblePath
then find1(maze,line,col-1);
This par is correct but the program terminates after the first exit it founds.
You have to figureout how to write that statement and since I think this is a school project I will not do it for you. I hope I did not help you to much.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.