Created
August 7, 2012 22:02
-
-
Save mshock/3289777 to your computer and use it in GitHub Desktop.
golf - dancing queens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# current solution using regex - 90 | |
#!perl -p0 | |
for$i(0,7..9){for$j(1..7){$c+=()=/(?=(?:_.{$i}){$j}Q|Q(?:.{$i}_){$j})/sg}}$_=$c | |
=pod | |
$/=0;$z=<>; | |
while($i++<7){ | |
$c+=()=$z=~/(?=$_)/sg for( | |
('_'.'.'x7)x$i.'Q', | |
('_'.'.'x9)x$i.'Q', | |
'Q'.('.'x7 .'_')x$i, | |
'Q'.(('.'x9 .'_')x$i), | |
('_'x$i).'Q', | |
'Q'.('_'x$i), | |
('_'.'.'x8)x$i.'Q', | |
'Q'.('.'x8 .'_')x$i | |
) | |
} | |
print$c | |
$/=0;$z=<>; | |
while($i<7){$i++; | |
@s = ( | |
('_' . '.' x 7) x $i . 'Q', | |
('_' . '.' x 9) x $i . 'Q', | |
'Q' . ('.' x 7 . '_') x $i, | |
'Q' . (('.' x 9 . '_') x $i), | |
('_' x $i) . 'Q', | |
'Q' . ('_' x $i), | |
('_' . '.' x 8) x $i . 'Q', | |
'Q' . ('.' x 8 . '_') x $i | |
); | |
map{$c+=()=$z=~/(?=$_)/sg}@s; | |
} | |
print $c; | |
chomp,$z.=$_ for<>; | |
while (0<=($x=index$z,'Q',$x)) { | |
push @z,$x++; | |
} | |
for(@z) { | |
$row_num = int($_ / 8); | |
$row_left = 8 * $row_num; | |
$row_right = $row_left + 7; | |
$col_top = $_ - ($row_num * 8); | |
$col_bot = $col_top + 8 * (8 - $row_num); | |
$j=$_; | |
while ($j >= $row_left) { | |
$j--; | |
last if $z[$j] == 'Q'; | |
$i++; | |
} | |
$j=$_; | |
while ($j <= $row_right) { | |
$j++; | |
last if $z[$j] == 'Q'; | |
$i++; | |
} | |
$j=$_; | |
while ($j>= $col_bot) { | |
$j-=8; | |
last if $z[$j] == 'Q'; | |
$i++; | |
} | |
$j=$_; | |
while ($j<=$col_top) { | |
$j+=8; | |
last if $z[$j] == 'Q'; | |
$i++; | |
} | |
while ($j<=$col_top && $j>=) { | |
} | |
} | |
chomp,push@z,[split'',$_]for<>; | |
func(); | |
print $i; | |
sub func { | |
my ($x0, $y0, $d) = @_; | |
for (my $y = 0; $y< 8; $y++) { | |
for (my $x = 0; $x < 8; $x++) { | |
#my $queen_flag = 1 if $z[$y][$x] eq 'Q'; | |
if ($z[$y][$x] eq 'Q') { | |
if ($d==1) { | |
} | |
if ($d==2) { | |
} | |
if ($d==3) { | |
} | |
if ($d==4) { | |
} | |
if ($d==5) { | |
} | |
if ($d==6) { | |
} | |
if ($d==7) { | |
} | |
if ($d==8) { | |
} | |
# next if ($x0 == $x) && ($y0 == $y); | |
if ($y0 == $y) { | |
$row_flag = 1; | |
} | |
elsif ($x0 == $x) { | |
$col_flag = 1; | |
} | |
elsif (abs(($y-$y0)/($x-$x0))==1) { | |
$diag_flag = 1; | |
} | |
else { | |
# test all directions | |
map {func($x, $y, $_)} [1..8]; | |
} | |
($row_flag, $col_flag, $diag_flag) = (0,0,0); | |
} | |
} | |
} | |
} | |
sub func { | |
my ($x0, $y0, $t) = @_; | |
for (my $y = 0; $y< 8; $y++) { | |
for (my $x = 0; $x < 8; $x++) { | |
my $queen_flag = 1 if $z[$y][$x] eq 'Q'; | |
if ($t) { | |
next if ($x0 == $x) && ($y0 == $y); | |
if ($y0 == $y) { | |
$row_flag = 1; | |
} | |
elsif ($x0 == $x) { | |
$col_flag = 1; | |
} | |
elsif (abs(($y-$y0)/($x-$x0))==1) { | |
$diag_flag = 1; | |
} | |
$left = 1 if $row_flag && $queen_flag && $x0 < $x; | |
$right = 1 if $row_flag && $queen_flag && $x0 > $x; | |
$top = 1 if $col_flag && $queen_flag && $y0 < $y; | |
$bot = 1 if $col_flag && $queen_flag && $y0 > $y; | |
$diag_tl = 1 if $diag_flag && $queen_flag && $x0 < $x && $y0 < $y; | |
$diag_tr = 1 if $diag_flag && $queen_flag && $x0 > $x && $y0 < $y; | |
$diag_bl = 1 if $diag_flag && $queen_flag && $x0 < $x && $y0 > $y; | |
$diag_br = 1 if $diag_flag && $queen_flag && $x0 > $x && $y0 > $y; | |
$i++ if $row_flag && !$left && $x0 < $x; | |
$i++ if $row_flag && !$right && $x0 > $x; | |
$i++ if $col_flag && !$top && $y0 < $y; | |
$i++ if $col_flag && !$bot && $y0 > $y; | |
$i++ if $diag_flag && !$diag_tl && $x0 < $x && $y0 < $y; | |
$i++ if $diag_flag && !$diag_tr && $x0 > $x && $y0 < $y; | |
$i++ if $diag_flag && !$diag_bl && $x0 < $x && $y0 > $y; | |
$i++ if $diag_flag && !$diag_br && $x0 > $x && $y0 > $y; | |
($row_flag, $col_flag, $diag_flag) = (0,0,0); | |
} | |
elsif ($queen_flag) { | |
func($x, $y, 1); | |
} | |
} | |
} | |
} | |
func(0,0); | |
print $c; | |
sub func { | |
my($i,$q)=@_; | |
# if this space is a queen | |
if($z[$i]=='Q'){ | |
# return if move search, start move search if queen search | |
if($q){ | |
return; | |
} | |
else { | |
# start search in all directions | |
func($i,1); | |
} | |
} | |
# traverse if doing move search | |
elsif($q){ | |
# this is another move | |
$c++; | |
# now continue in this direction or stop if at edge | |
} | |
else {# otherwise, advance to the next square in queen search | |
func($i+1,0); | |
} | |
} | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
all possible moves for queens on a chessboard