Thanks for your reply, Roy.
This is a really good solution, and I like it a lot. It's also _really_ fast.
However, I was feeding a couple of "real life" books through it, and curiously, it does seem a little "keen" to use the same layouts consecutively.
If I feed in a book of "llpllplppllpplplpplpplpllplplp" with $desired_page_count = 13, your regexp routine returns a book featuring layouts "12,01,07,06,02,08,02,03,04,04,03,03,03" - valid, certainly, but the same layouts repeated at the end.
However, my sloooooow routine returns "01,04,03,05,01,02,08,11,02,03,01,07,03", without repeating layouts.
I assume (guess alert!) it's because the regexp routine is happy to proceed along a certain route as long as solutions exist - not necessarily "good" (by my definition!) solutions. Also, as you get nearer the end of the book, there are fewer valid layout combinations.
Does anyone have any suggestions for tweaking this behaviour?
In reply to Re^2: Algorithm to fit photos into spaces on pages