LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2006, 04:04 AM   #1
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Rep: Reputation: 15
Pascal Programming problem


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 !
 
Old 08-13-2006, 05:42 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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 '|'.
 
Old 08-13-2006, 04:36 PM   #3
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Original Poster
Rep: Reputation: 15
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);
 
Old 08-14-2006, 08:32 AM   #4
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
Quote:
Originally Posted by linuxlover1
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.

Last edited by kalleanka; 08-14-2006 at 08:35 AM.
 
Old 08-15-2006, 12:48 PM   #5
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Original Poster
Rep: Reputation: 15
hey man...heh...its not a school project..i just want to learn recursion !
by the way it finds all ways !
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Free Pascal ubuntuubuntu Linux - Software 4 02-28-2006 09:16 PM
pascal to C problem in struct storing data ngan_yine Programming 6 01-25-2005 03:21 AM
Problem with Java and Pascal usr Programming 6 05-19-2004 01:32 PM
C Programming examples including linking with Pascal chris.hicks Programming 0 11-22-2003 04:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration