• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Two A-maze-ing Programs

    December 17, 2008 Ted Holt


    Note: The code accompanying this article is available for download here.

    My family and I are preparing to celebrate Christmas. I’m enjoying the lights, the carols, and the decorations. I feel festive. I feel like doing something just for fun. Thus, the two programs I wrote for today’s Four Hundred Guru are of no practical value of which I’m aware, but goodness, did I have fun writing them!

    Consider the following text file, the contents of which is a maze.

    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    IB             I         I I I         I
    IIIIIIIIIIIIII I IIIIIII I I   II IIIIII
    II             I I       I I I I       I
    II IIIIIIIIIIIII I IIIIIII I I IIIIIIIII
    I  I             I I       I I         I
    I I  IIIIIIIII III I IIIII I IIIIIIIII I
    I   I        I I   I     I           I I
    IIIII IIIIIIII I IIIIIII IIIIIIIIIIIII I
    I              I         I             I
    I IIIIIIIIIIIIIIIIIIIIII I IIIIIII I III
    I       I   IIII       I I I       I I I
    I I III I I I IIIIIIII   I I IIIII I I I
    I I I   I I I  I       I I I I     I   I
    I IIIIIII IIII IIIIIIIIIII I IIIIIIIIIII
    I          IE              I           I
    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    

    The beginning and ending points are cleverly indicated by the letters “B” and “E”, respectively. How does one go about writing a computer program that finds a path from point B to point E?

    A Recursive Method

    One way to tackle this problem is to use recursion. Starting at point B, try to move up. If that way is blocked or turns out to be a dead end, try to move left. If that way is blocked or turns out to be a dead end, try to move right. If that way is blocked or turns out to be a dead end, try to move down.

    All of this logic is implemented in a short routine, which in my program is called “Try”. Try takes three parameters–a row number, a column number, and whether or not a solution has been found.

    P Try             b                               
    D                 pi                              
    D   Row                          5i 0 value       
    D   Col                          5i 0 value       
    D   Solved                        n               
    D*** locals                                       
    D CurrentChar     s              1a               
     /free                                            
        CurrentChar = %subst(Maze: Offset(Row:Col):1);
        select;                                       
        when CurrentChar = 'E';                       
           Solved = *on;
           PrintMaze();                               
           return;                                    
        when CurrentChar = *blank                     
        or   CurrentChar = 'B';                       
           Mark(Row: Col);                            
           Try (Row: Col: Solved);                    
           if not Solved;                             
              Try (Row-1: Col: Solved);   // look up  
           endif;                                     
           if not Solved;                             
              Try (Row: Col-1: Solved);   // look left
           endif;                                     
           if not Solved;                             
              Try (Row: Col+1: Solved);   // look right
           endif;                                      
           if not Solved;                              
              Try (Row+1: Col: Solved);   // look down 
           endif;                                      
           UnMark(Row: Col);                           
        endsl;                                         
     /end-free                                         
    P                 e                                
    

    How can something so short and so simple solve such a complicated problem?

    Notice that Try calls itself. In this example, the call to Try (2, 2) calls Try (2, 3), which calls Try (2, 4) and so on. Whenever an invocation of Try runs into a dead end, it backtracks and tries the next direction. Eventually it finds the E and prints the maze.

    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    I**************I*********I I I         I
    IIIIIIIIIIIIII*I*IIIIIII*I I***II IIIIII
    II*************I*I*******I I*I*I       I
    II*IIIIIIIIIIIII*I*IIIIIII I*I*IIIIIIIII
    I**I*************I*I*******I*I*********I
    I*I**IIIIIIIII III*I*IIIII*I*IIIIIIIII*I
    I***I        I I***I*****I***        I*I
    IIIII IIIIIIII I*IIIIIII*IIIIIIIIIIIII*I
    I              I*********I*************I
    I IIIIIIIIIIIIIIIIIIIIII I*IIIIIII I III
    I       I   IIII       I I*I       I I I
    I I III I I I IIIIIIII   I*I IIIII I I I
    I I I   I I I  I       I I*I I     I   I
    I IIIIIII IIII IIIIIIIIIII*I IIIIIIIIIII
    I          IE**************I           I
    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    

    Notice that the path to the end is marked with asterisks.

    An Iterative Method

    Several years ago, I learned an iterative way to solve mazes from a Dr. Dobbs Journal article. I wish I could find the article in order to give proper credit to the author, but I can’t, and my Google search only turned up references to Basem A. Nayfeh’s cellular automata method, which I’m quite sure is not the article I read.

    Anyway, here’s the idea. Beginning at point B, examine each square, row by row, column by column. If a blank square is surrounded on three sides by wall or dead end, mark the square as a dead end. An iteration ends when you reach the end of the maze. Continue until an iteration finds no dead ends.

    Consider the following portion of a maze. The last blank in the second row is surrounded by three squares of wall, which means that it’s a dead end.

    IIIIIIIIII
    I        I
    I IIIIIIII
    

    After the first iteration, that square has been marked with an X as a dead end.

    IIIIIIIIII
    I       XI
    I IIIIIIII
    

    The square before the new dead end, which is now the last blank square on the second line, is surrounded by two walls and a dead end. The second iteration flags this square as a dead end, and the maze looks like this.

    IIIIIIIIII
    I      XXI
    I IIIIIIII
    

    And so it goes. Here is the final state of the maze. Follow the blank squares to find the path to the end.

    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    IB             I         IXIXIXXXXXXXXXI
    IIIIIIIIIIIIII I IIIIIII IXI   IIXIIIIII
    II             I I       IXI I IXXXXXXXI
    II IIIIIIIIIIIII I IIIIIIIXI I IIIIIIIII
    I  I             I I       I I         I
    I I  IIIIIIIIIXIII I IIIII I IIIIIIIII I
    I   IXXXXXXXXIXI   I     I   XXXXXXXXI I
    IIIIIXIIIIIIIIXI IIIIIII IIIIIIIIIIIII I
    IXXXXXXXXXXXXXXI         I             I
    IXIIIIIIIIIIIIIIIIIIIIIIXI IIIIIIIXIXIII
    IXXXXXXXIXXXIIIIXXXXXXXIXI IXXXXXXXIXIXI
    IXIXIIIXIXIXIXIIIIIIIIXXXI IXIIIIIXIXIXI
    IXIXIXXXIXIXIXXIXXXXXXXIXI IXIXXXXXIXXXI
    IXIIIIIIIXIIIIXIIIIIIIIIII IXIIIIIIIIIII
    IXXXXXXXXXXIE              IXXXXXXXXXXXI
    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    

    My Programs

    My code is available for download . I quickly threw it together, so it is not elegant, and I’m sure you can improve on it, because I know I could.

    There are two RPG programs, MAZERECUR and MAZELOOP, which implement the recursive and iterative methods. You will also find a file containing the maze I used in the examples.

    Before you call the programs, you’ll have to create a maze in a source physical file member. You can use SEU or WDSC to enter the maze. The rows can’t contain more than eighty characters. You can have as many rows as you like, as long as the number of rows times the number of columns does not exceed 32K.

    After you compile the programs, call a program with the qualified name of the file containing the maze in the first parameter and the name of the member containing the maze in the second parameter. For instance, if you put the maze in member MYMAZE in source physical file MYLIB/QRPGLESRC, these are the calls you can use.

    CALL MAZERECUR ('MYLIB/QRPGLESRC' MYMAZE)
    CALL MAZELOOP ('MYLIB/QRPGLESRC' MYMAZE)
    

    Have fun! Merry Christmas!



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    PowerTech:  Incorporating real-time security events from the System i into a security program
    Safedata:  FREE White Paper - IBM iSeries Recovery Options: An Executive Guide
    COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    Getting Started with PHP for i5/OS: List Price, $59.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    IBM Seeks Organic Solution to Power Systems Challenge, Global Warming Now What?

    Leave a Reply Cancel reply

Volume 8, Number 43 -- December 17, 2008
THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
Group8 Security

Table of Contents

  • Two A-maze-ing Programs
  • End-of-Year Odds and Ends
  • Admin Alert: Upcoming i5/OS and AnyNet End of Service Dates

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle