queens.a68

     
   1  CO
   2  
   3  @section Synopsis
   4  
   5  'N' queens in classic backtracker.
   6  
   7  CO
   8  
   9  BEGIN
  10  
  11     INT rank = 8;
  12     printf (($"Rank="g(0)l$, rank));
  13     [1 : rank] CCOL column, INT sols found := 0;
  14  
  15     PROC place from  = (CROW this row) VOID:
  16          IF this row > rank
  17          THEN sols found +:= 1;
  18               FOR row TO rank
  19               DO print(("abcdefghijklmnopqrstuvwxyz"[row], whole(column[row], 0), " "))
  20               OD;
  21               print ((sols found MOD 4 = 0 | new line | "  "))
  22          ELSE FOR i TO rank
  23               DO IF BOOL safe := TRUE;
  24                     FOR j TO this row - 1 
  25                     WHILE safe
  26                     DO safe := safe AND NOT (column[j] = i ORF (column[j] - i = this row - j ORF column[j] - i = j - this row))
  27                     OD;
  28                     safe
  29                  THEN column[this row] := i;
  30                       place from (this row + 1)
  31                  FI
  32               OD
  33          FI;
  34               
  35     place from (1);
  36  
  37     MODE CROW = INT, CCOL = INT;
  38  
  39     SKIP
  40  
  41  END
     


© 2002-2024 J.M. van der Veer (jmvdveer@xs4all.nl)