Task

For a maze generated by this task, write a function
that finds (and displays) the shortest path between two cells.



Note that because these mazes are generated by the Depth-first search algorithm, they contain no circular paths,
and a simple depth-first tree search can be used.




      MazeWidth% = 11
      MazeHeight% = 9
      MazeCell% = 50
 
      VDU 23,22,MazeWidth%*MazeCell%/2+3;MazeHeight%*MazeCell%/2+3;8,16,16,128
      VDU 23,23,3;0;0;0; : REM Line thickness
      OFF
      PROCgeneratemaze(Maze&(), MazeWidth%, MazeHeight%, MazeCell%)
      PROCsolvemaze(Path{()}, Maze&(), 0, MazeHeight%-1, MazeWidth%-1, 0, MazeCell%)
      END
 
      DEF PROCsolvemaze(RETURN s{()}, m&(), x%, y%, dstx%, dsty%, s%)
      LOCAL h%, i%, n%, p%, q%, w%
      w% = DIM(m&(),1)
      h% = DIM(m&(),2)
      DIM s{(w%*h%) x%,y%}
      GCOL 3,14
      m&(x%,y%) OR= &80
      REPEAT
        FOR i% = 0 TO 3
          CASE i% OF
            WHEN 0: p% = x%-1 : q% = y%
            WHEN 1: p% = x%+1 : q% = y%
            WHEN 2: p% = x% : q% = y%-1
            WHEN 3: p% = x% : q% = y%+1
          ENDCASE
          IF p% >= 0 IF p% < w% IF q% >= 0 IF q% < h% IF m&(p%,q%) < &80 THEN
            IF p% > x% IF m&(p%,q%) AND 1 EXIT FOR
            IF q% > y% IF m&(p%,q%) AND 2 EXIT FOR
            IF x% > p% IF m&(x%,y%) AND 1 EXIT FOR
            IF y% > q% IF m&(x%,y%) AND 2 EXIT FOR
          ENDIF
        NEXT
        IF i% < 4 THEN
          m&(p%,q%) OR= &80
          s{(n%)}.x% = x%
          s{(n%)}.y% = y%
          n% += 1
        ELSE
          IF n% > 0 THEN
            n% -= 1
            p% = s{(n%)}.x%
            q% = s{(n%)}.y%
          ENDIF
        ENDIF
        LINE (x%+0.5)*s%,(y%+0.5)*s%,(p%+0.5)*s%,(q%+0.5)*s%
        x% = p%
        y% = q%
      UNTIL x%=dstx% AND y%=dsty%
      s{(n%)}.x% = x%
      s{(n%)}.y% = y%
      ENDPROC
 
      DEF PROCgeneratemaze(RETURN m&(), w%, h%, s%)
      LOCAL x%, y%
      DIM m&(w%, h%)
      FOR y% = 0 TO h%
        LINE 0,y%*s%,w%*s%,y%*s%
      NEXT
      FOR x% = 0 TO w%
        LINE x%*s%,0,x%*s%,h%*s%
      NEXT
      GCOL 15
      PROCcell(m&(), RND(w%)-1, y% = RND(h%)-1, w%, h%, s%)
      ENDPROC
 
      DEF PROCcell(m&(), x%, y%, w%, h%, s%)
      LOCAL i%, p%, q%, r%
      m&(x%,y%) OR= &40 : REM Mark visited
      r% = RND(4)
      FOR i% = r% TO r%+3
        CASE i% MOD 4 OF
          WHEN 0: p% = x%-1 : q% = y%
          WHEN 1: p% = x%+1 : q% = y%
          WHEN 2: p% = x% : q% = y%-1
          WHEN 3: p% = x% : q% = y%+1
        ENDCASE
        IF p% >= 0 IF p% < w% IF q% >= 0 IF q% < h% IF m&(p%,q%) < &40 THEN
          IF p% > x% m&(p%,q%) OR= 1 : LINE p%*s%,y%*s%+4,p%*s%,(y%+1)*s%-4
          IF q% > y% m&(p%,q%) OR= 2 : LINE x%*s%+4,q%*s%,(x%+1)*s%-4,q%*s%
          IF x% > p% m&(x%,y%) OR= 1 : LINE x%*s%,y%*s%+4,x%*s%,(y%+1)*s%-4
          IF y% > q% m&(x%,y%) OR= 2 : LINE x%*s%+4,y%*s%,(x%+1)*s%-4,y%*s%
          PROCcell(m&(), p%, q%, w%, h%, s%)
        ENDIF
      NEXT
      ENDPROC