Keep It Simple, Stupid PerlMonks

### Re: Better algorithm for Number Place Puzzle

by dash2 (Hermit)
 on May 23, 2004 at 22:20 UTC ( #355777=note: print w/replies, xml ) Need Help??

in reply to Better algorithm for Number Place Puzzle

Just a guess. Perhaps you could use the other monk's blanked out solution to create an initial grid. Then, recreate this solution in the other grids, but transform it in some simple way in order to make sure it doesn't break the rules. My shot - just a first guess - is below, hidden:
 Hmm. Let's try a 2 x 2 version. The initial grid is ```12 34 [download]``` obviously. Now you need to fill in ```12-- 34-- ---- ---- [download]``` Clearly, as you move across, you need to shift rows: ```1234 3412 ---- ---- [download]``` And as you move down, you need to shift columns: ```1234 3412 21-- 43-- [download]``` note that the lower left grid is just the upper left grid with columns shifted along. Finally, ```1234 3412 2143 4321 [download]``` it turns out that the column shift from the grid above, and the row shift from the grid to the left, give identical results.
Perhaps this will work with N>2. Or not. Enjoy.
 A massive flamewar beneath your chosen depth has not been shown here

Replies are listed 'Best First'.
Re: Re: Better algorithm for Number Place Puzzle
by blokhead (Monsignor) on May 23, 2004 at 23:27 UTC
This can be extended to create a random solution, just randomize the initial block. (This is similar to Abigail-II's solution). Here @start is a N by N matrix whose entries are a random permutation of {1, .., N^2}.
```my \$n     = shift || 3;

my @start = do {
use List::Util 'shuffle';
my @perm = shuffle 1 .. \$n*\$n;

map {
my \$row = \$_;
[ map { \$perm[\$row * \$n + \$_] } 0 .. \$n-1 ]
} 0 .. \$n-1;
};

my @solution;

for my \$x ( 0 .. \$n*\$n - 1 ) {
for my \$y ( 0 .. \$n*\$n - 1 ) {

my \$block_x = int (\$x / \$n);
my \$block_y = int (\$y / \$n);

\$solution[\$x][\$y] =
\$start[ (\$block_x + \$y) % \$n ][ (\$block_y + \$x) % \$n ];
}
}

print join "" => map { "@\$_\n" } @solution;

Create A New User
Node Status?
node history
Node Type: note [id://355777]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-03-24 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (297 votes). Check out past polls.

Notices?