#!/usr/bin/perl -w use strict; # Se trata de poner 8 reinas en un casillero de ajedrez alarm 60*30; # 30 minutos my (@c, $sol, $ok, $m, $n, @queen, $it, $string, %hash); $sol = 0; while (1){ # ver abajo. Ya se conoce el nĂºmero para n x n $sol++; loop: @queen = (); for my $i (1..8){ $c[$i][$_]=0 for (1..8); } $it=0; for my $q (1..8){ loop2: $m = int 1+rand(8); $n = int 1+rand(8); if ($c[$m][$n] == 0){ $it=0; tacha($m,$n); push @queen , $m.",".$n; }elsif (++$it > 81*81){ goto loop; # esto ya contiene holgura }else{ goto loop2; } } $string = join " ", sort @queen; $hash{ $string }++; goto loop if ($hash{ $string } > 1); # FAIL print "$sol $string\n"; } exit 1; sub tacha{ my ($i,$j) = @_; $c[$_][$j] = 1 for (1 .. 8); $c[$i][$_] = 1 for (1 .. 8); for my $d (1..7){ if (defined $c[$i-$d][$j-$d]) { $c[$i-$d][$j-$d] = 1; } if (defined $c[$i-$d][$j+$d]) { $c[$i-$d][$j+$d] = 1; } if (defined $c[$i+$d][$j-$d]) { $c[$i+$d][$j-$d] = 1; } if (defined $c[$i+$d][$j+$d]) { $c[$i+$d][$j+$d] = 1; } } } __END__ Hay 92 configuraciones en 8x8. Hay 23 configuraciones en 23x23 y 1000 configuraciones en 1000x1000.