HP Forums
Re: a challenge related to the 15 puzzle - Printable Version

+- HP Forums (https://archived.hpcalc.org/museumforum)
+-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html)
+--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html)
+--- Thread: Re: a challenge related to the 15 puzzle (/thread-177302.html)

Re: a challenge related to the 15 puzzle - Don Shepherd - 01-20-2011

I figured out that, rather than have two loops to calculate the inversion count (which takes about 40 seoonds on the 32sii), you can actually do it while you are entering the numbers if you keep track of the numbers you have previously entered. Then, when you enter the last number, you can see the inversion count immediately. This 32sii program implements that algorithm.

This version of the 15 puzzle solvability program is for the 32sii and not the 32s
because it uses the exchange x with a register command which is not on the 32s.
Since I got the 32sii I've been looking for a reason to use this command
and this is it!

This version calculates the cumulative inversion count as the 16 numbers are entered, so
once you enter the last number it tells you the inversions instantly, with no waiting.


enter first number
enter second number
enter 16th number

it then displays the inversion count; even number means the puzzle can be solved.


A-O - set to 1 when the corresponding number is entered
P - inversion count (even means solvable)
Q - main loop index
i - indirect addressing and second loop index


lbl a entry point
1.016 initialize loop to enter 16 numbers
sto q main loop index
lbl b begin main loop
r/s enter the next number (0 for empty space)
x=/=0 if empty space, calc row number and update P
goto c for 1-15
sto+p update inversion count with row # of empty cell
goto x iterate main loop
lbl c handle 1-15
x<->i move # to i
sto (i) put a 1 in A-O when number is found
x<->i get number back in x
1 add x-1 to p, max possible inversions
x=0 if 0 this is number 1, so nothing can be to it's left
goto x so iterate main loop
1000 otherwise loop from 1 to (x-1) and subtract what
/ you find there from p
sto i so input number 7 would cause loop from 1 to 6
lbl d begin loop
rcl(i) will be 0 or 1
sto-p subtract from inversion count
isg i iterate loop
goto d
lbl x iterate main loop now
isg q
goto b continue main loop to enter next number
rcl p display inversion count
rtn all done