|Re: a challenge related to the 15 puzzle|
Message #1 Posted by Don Shepherd on 19 Jan 2011, 4:53 a.m.
Well, the HP-32sii version takes a while to run because it has to decode registers A-D in which 4 numbers are stored in each register. I did that to avoid using so many registers. This version stores a single cell value in each register A-P. While it uses many more registers than the other version, it runs much quicker (about 7 seconds versus 40 seconds).
Life (and programming) is all about trade-offs!
Program to determine solvability of 15 puzzle for 32s.
This one is faster than the one that stores 4 numbers in each register A-D.
A-P - 16 cell values, entered after XEQ A
Q - number of inversions, including row number of empty cell
R - current outer loop cell value
i - indirect addressing and inner loop index
T - outer loop index
enter 16 cell values (0 for empty cell) R/S after each
program displays number of inversions; if even, puzzle is solvable
lbl a entry point
1.016 loop to get cell values
r/s enter each of 16 values
sto (i) stores in A-P
x<>0 if 0 calc row#
sto q inversion count starts with row# of empty cell
1.015 outer loop goes from 1 to 15
sto t outer loop index
rcl t get current outer loop cell value
sto i and store in R
+ inner loop goes from t+1 to 16
sto i inner loop index
x=0 if inner loop cell value is 0, ignore
rcl r current outer loop cell value
sto + q increment inversion counter
goto e iterate inner loop
goto d iterate outer loop
rcl q display inversion counter, even means puzzle is solvable
Edited: 19 Jan 2011, 10:23 a.m.