All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > General
5 x 5 Maze by 2s and 3s (Posted on 2009-03-22) Difficulty: 3 of 5
In the grid below, start at the A and end at the W, by first moving two positions (horizontally, vertically or diagonally), then by three positions, etc., alternating between 2- and 3-position moves. Never go to the same letter twice (though you may cross over a previous letter) and never reverse directions 180 degrees from one move to the next. Also never land on or even jump over the two hyphens.

For example, a valid start is A-C-P-R, and then the only valid move is the diagonal to B, as you can't backtrack to O, nor go up past the hyphen, nor is there enough room to go down three positions, nor any space to the right. Diagonals are always allowed, not just when you're stuck like this.

A B C D E
F G H I J
K L M N -
- O P Q R
S T U V W
What is the shortest such path? ... and the longest?

  Submitted by Charlie    
Rating: 4.0000 (1 votes)
Solution: (Hide)
The following are the valid paths:

aknboivtjhuw
akntw
aknvfhusigw
aknvfhuw
acpfiorhukntw
acpfiorhuw
acpjgorhukntw
acpjgorhuw
acprbdqgtvfhuw

The shortest is akntw (4 moves), and the longest is acprbdqgtvfhuw (13 moves).

DECLARE SUB move ()
DIM SHARED x, y, h$, b$(5, 5), hx(25), hy(25), hdx(25), hdy(25), mSize
DATA a,b,c,d,e, f,g,h,i,j, k,l,m,n,"-","-",o,p,q,r,s,t,u,v,w
FOR r = 1 TO 5: FOR c = 1 TO 5
  READ b$(r, c)
  PRINT b$(r, c);
NEXT: PRINT : NEXT

PRINT
x = 1: y = 1: h$ = "a": hx(1) = 1: hy(1) = 1
hdx(1) = 0: hdy(1) = 0

mSize = 2: move

END

SUB move
 mNo = LEN(h$)
 FOR dx = -1 TO 1
 FOR dy = -1 TO 1
  IF dx <> 0 OR dy <> 0 THEN
   IF dx <> -hdx(mNo) OR dy <> -hdy(mNo) THEN
    newX = hx(mNo) + dx * mSize: newY = hy(mNo) + dy * mSize
    IF newX > 0 AND newX <= 5 AND newY > 0 AND newY <= 5 THEN
     IF INSTR(h$, b$(newY, newX)) = 0 THEN
      x1 = hx(mNo) + dx: y1 = hy(mNo) + dy
      x2 = hx(mNo) + 2 * dx: y2 = hy(mNo) + 2 * dy
      IF b$(y1, x1) <> "-" AND b$(y2, x2) <> "-" AND b$(newY, newX) <> "-" THEN
        mNo = mNo + 1
        mSize = 5 - mSize
        h$ = h$ + b$(newY, newX)
        hx(mNo) = newX: hy(mNo) = newY
        hdx(mNo) = dx: hdy(mNo) = dy
        IF b$(newY, newX) = "w" THEN
          PRINT h$
        ELSE
          move
        END IF
       
        h$ = LEFT$(h$, LEN(h$) - 1)
        mSize = 5 - mSize
        mNo = mNo - 1
      END IF
     END IF
    END IF
   END IF
  END IF
 NEXT
 NEXT
END SUB
From the Page-A-Day 2009 Mensa 365 Brain Puzzlers Calendar, for Thursday Jan. 1. Workman Publishing Co., by Dr. Abbie F. Salny, Mark Danna and Fraser Simpson.

Comments: ( You must be logged in to post comments.)
  Subject Author Date
Puzzle AnswerK Sengupta2023-07-21 00:36:45
re(4): SolutionHarry2009-03-27 20:02:48
re(3): SolutionCharlie2009-03-27 11:17:20
re(2): SolutionHarry2009-03-24 19:38:10
re: SolutionCharlie2009-03-23 10:41:27
re: Solution (spoiler)Dej Mar2009-03-22 23:26:39
SolutionSolutionHarry2009-03-22 22:14:30
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (1)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (15)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information