in reply to How to debug a long while loop
I don't want to solve a homework problem for you, especially as you've made such a great start at it. Here's a hint that may help you out:
Unless you're specifically avoiding it, recursion would be your best bet here. You're doing a lot of work in your code that recursion would "automatically" take care of for you. You're making a complex data structure (board) to track a handful of state variables that you would get "free" using recursion.
For example, your board array is rather complicated. You could use local variables to store most of your state information for each move. Something like:
Here, rather than putting the $move variable into your board structure, you're letting perl keep track of them for you on the stack. As you call solve, you get a new copy of $move, and when you return, you get the previous value back.sub solve { my $move = shift; ... other code ... solve($move+1,...); }
Using local variables and recursion, you should be able to use an 8x8 array of integers to track which squares have been used, the rest being local variables.
If you need another hint or two, let me know.!
(Note: Just as an example of how using recursion can simplify things a bit, I cobbled together a quickie solution that's a little less than half the size of yours.)
...roboticus
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Queen's problem (was Re^2: How to debug a long while loop)
by roboticus (Chancellor) on May 17, 2007 at 14:22 UTC |