The following generates the solution using brute force. It works for 3x3, but takes forever for 10x10. It hasn't finished running for 10x10 yet, so I don't know if it gives a sensible answer.
use strict;
use warnings;
use re 'eval';
my $size = 3;
my $sizem1 = $size-1;
my $sizem2 = $size-2;
my $grid = 'X' x ($size*$size);
my %solution;
my @todo = $grid;
while (@todo) {
local $_ = pop(@todo);
next if $solution{$_}++;
# Row
/^((?:.{$size})*X{0,$size})X(X{0,$sizem1}(?:.{$size})*)$
(?{ push(@todo, "$1_$2") })(?=\Z=)/x;
# Col
/^(.{0,$sizem1}(?:X.{$sizem1})*)X((?:.{$sizem1}X)*.{0,$sizem1})$
(?{ push(@todo, "$1_$2") })(?=\Z=)/x;
# Diag \
/^((?:X.{$size})*)X((?:.{$size}X)*)$
(?{ push(@todo, "$1_$2") })(?=\Z=)/x;
# Diag /
/^(.{$sizem1}(?:X.{$sizem2})*)X((?:.{$sizem2}X)*.{$sizem1})$
(?{ push(@todo, "$1_$2") })(?=\Z=)/x;
}
my @solutions = map substr($_, 5),
sort { $b cmp $a }
map sprintf('%05d%s', 0+/_/g, $_),
keys %solution;
foreach my $solution (@solutions) {
print
map "$_\n\n",
join "\n",
map /(...)/g,
$solution;
}
A mirror image of a solution is counted as a solution.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.