Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

obviously. Now you need to fill in

12-- 34-- ---- ----

Clearly, as you move across, you need to shift rows:

1234 3412 ---- ----

And as you move down, you need to shift columns:

1234 3412 21-- 43--

note that the lower left grid is just the upper left grid with columns shifted along. Finally,

1234 3412 2143 4321

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;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://355777]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-06-19 21:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (115 votes). Check out past polls.