LyWang

8_Queen_Problem

Nov 13th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.67 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5. my @solution = ([], [], [], [], [], [], [], []); #board
  6.  
  7. sub initial {
  8.   for my $i (0 .. 7) {
  9.     for (0 .. 7) {push @{$solution[$i]}, 0;} #create the board
  10.   }
  11. }
  12.  
  13. sub safe {
  14.   my $row_safe = shift;
  15.   my $col_safe = shift;
  16.   if ($solution[$col_safe]->[$row_safe] == 0) {return 1;}
  17.   else {return 0;}
  18. }
  19.  
  20. sub mark {
  21.   my $row_mark = shift;
  22.   my $col_mark = shift;                                                              #get current col
  23.   for my $c ($col_mark .. 7) {                                                       #from this col to the las col
  24.     for my $r (0 .. 7) {                                                             #for each row
  25.       if ($r == $row_mark && $c == $col_mark) {$solution[$c]->[$r] = $col_mark + 1;}
  26.       #original answer will be marked as col+1
  27.       elsif ($solution[$c]->[$r] == 0) {
  28.         if ($r == $row_mark) {$solution[$c]->[$r] = 10 * ($col_mark + 1);}
  29.         if ($r - $c == $row_mark - $col_mark) {$solution[$c]->[$r] = 10 * ($col_mark + 1);}
  30.         if ($r + $c == $row_mark + $col_mark) {$solution[$c]->[$r] = 10 * ($col_mark + 1);}
  31.       }#we check if the value there is 0 #we write 10* the original col+1, as attack area
  32.       else {} # we do nothing if anything else
  33.     }
  34.   }
  35. }
  36.  
  37. sub unmark {
  38.   my $row_unmark = shift;
  39.   my $col_unmark = shift;                                                    #get current col
  40.   for my $c ($col_unmark .. 7) {                                             #from this col to the las col
  41.     for my $r (0 .. 7) {                                                     #for each row
  42.       if ($r == $row_unmark && $c == $col_unmark) {$solution[$c]->[$r] = 0;} #original answer will be marked as col+1
  43.       elsif ($solution[$c]->[$r] == 10 * ($col_unmark + 1)) {                #we check if the value there is 0
  44.         $solution[$c]->[$r] = 0;}
  45.       #we write 10* the original col+1, as attack area
  46.       else {} # we do nothing if anything else
  47.     }
  48.   }
  49. }
  50.  
  51. sub solve {
  52.   my $col_solve = shift; #for the col where we are
  53.   if ($col_solve == 8) {
  54.     process();
  55.     return 1;
  56.   }
  57.   for my $row_solve (0 .. 7) { #for every row we are going to try
  58.     if (safe($row_solve, $col_solve)) {
  59.       mark($row_solve, $col_solve);
  60.       solve($col_solve + 1);
  61.       unmark($row_solve, $col_solve);
  62.     }
  63.   }
  64. }
  65.  
  66. sub process {
  67.   for my $rp (0 .. 7) {
  68.     for my $cp (0 .. 7) {
  69.       if ($solution[$rp]->[$cp] > 0 && $solution[$rp]->[$cp] < 10) {print "Q";} #create the board
  70.       else {print "口";} #create the board
  71.     }
  72.     print "\n";
  73.   }
  74.   print "\n\n";
  75. }
  76.  
  77. sub main {
  78.   initial();
  79.   solve(0);
  80. }
  81.  
  82. main();
Add Comment
Please, Sign In to add comment