home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / insidetp / 1990_04 / queens.pas < prev    next >
Pascal/Delphi Source File  |  1990-03-19  |  2KB  |  64 lines

  1. {************************************************
  2. * QUEENS - solve the classical 'Queens' problem *
  3. * FOR a board OF size n.  The problem is TO put *
  4. * n Queens (chess pieces) on an n x n board so    *
  5. * that no queen can attack any OF The others.    *
  6. * This IMPLEMENTATION will print The number OF    *
  7. * solutions found FOR each board size, from 1    *
  8. * TO 9 squares on a side.                       *
  9. *                                               *
  10. * 900125 MCMason - originally coded             *
  11. *************************************************}
  12.  
  13. {The Queen1 unit initially solves the puzzle,
  14.  substitute queen2 and queen3 in the $Define
  15.  compiler directive for alternative algorithms}
  16.  
  17.  
  18. {$DEFINE QUEEN1}
  19.  
  20. {$IFDEF QUEEN1}
  21. USES QUEEN1;
  22. {$ENDIF}
  23.  
  24. {$IFDEF QUEEN2}
  25. USES QUEEN2;
  26. {$ENDIF}
  27.  
  28. {$IFDEF QUEEN3}
  29. USES QUEEN3;
  30. {$ENDIF}
  31.  
  32.  
  33. VAR
  34.    TotalSolns : Integer;   {Total solns/all sizes}
  35.    I : Integer;
  36.  
  37.  
  38. BEGIN
  39.    TotalSolns := 0;
  40.  
  41.    { Print table heading }
  42.  
  43.    WriteLn('# Queens    # solns    Total #');
  44.    WriteLn('# Brd sz    this sz     solns');
  45.    WriteLn('--------    -------    -------');
  46.  
  47.    {Find the number of solutions for each}
  48.    {board size, and print the table entry.}
  49.  
  50.    FOR I :=1 TO 9 DO
  51.       BEGIN
  52.          InitBoard;          {Clear the board}
  53.                              {Solve the problem}
  54.          Queens(0,I);        {Start @ column 0}
  55.                              {Print summary}
  56.          Inc(TotalSolns,solns);
  57.          WriteLn(I:6,solns:13,TotalSolns:10)
  58.       END;
  59.  
  60.    {Print end of table footer}
  61.  
  62.    WriteLn('--------    -------    -------')
  63. END.
  64.