home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / U < prev    next >
Encoding:
Text File  |  1992-03-30  |  1.9 KB  |  72 lines

  1. /* This queens program allows the board size to be specified    */
  2. /* when the program begins execution.                */
  3.  
  4. /* The next 4 lines are for the benefit of Lattic C, which lacks
  5.    the "void" type or enumerations.
  6.  
  7. #define void     /* */
  8. #define False 0
  9. #define True 1
  10. #define Boolean char
  11.  
  12. #include <stdio.h>
  13.  
  14. typedef int Integer;
  15.  
  16. static Boardsize=0;
  17.  
  18. #define MaxBoardsize 20
  19.  
  20. #define Asub(I)  A[(I)-1]
  21. #define Bsub(I)  B[(I)-2]
  22. #define Csub(I)  C[(I)+MaxBoardsize-1]
  23. #define Xsub(I)  X[(I)-1]
  24.  
  25. static Boolean A[MaxBoardsize /* 1. .MaxBoardsize */];
  26. static Boolean B[MaxBoardsize+MaxBoardsize-1 /* 2..(MaxBoardsize+MaxBoardsize) */];
  27. static Boolean C[MaxBoardsize+MaxBoardsize-1 /*-(MaxBoardsize-1)..(MaxBoardsize-1) */];
  28. static Integer X[MaxBoardsize /* 1.. MaxBoardsize */];
  29.  
  30. /* Statistics: */
  31. static long Trials=0;
  32.  
  33. void Try(I,Q) Integer I; Boolean *Q; {
  34.    Integer J = 0;
  35.    Trials++;
  36.    do {
  37.       J++; *Q = False;
  38.       if (Asub(J) && Bsub(I+J) && Csub(I-J)) {
  39.      Xsub(I) = J;
  40.      Asub(J) = False; Bsub(I+J) = False; Csub(I-J) = False;
  41.      if (I < Boardsize) {
  42.         Try(I+1,Q);
  43.         if (!*Q) {
  44.            Asub(J) = True; Bsub(I+J) = True; Csub(I-J) = True;
  45.            }
  46.         }
  47.      else *Q = True;
  48.      }
  49.       }
  50.    while (!(*Q || J==Boardsize));
  51.    }
  52.  
  53. void main () {
  54.    Integer J,I,iter; Boolean Q;
  55.    printf("Boardsize; #iterations?");
  56.    scanf("%d %d",&Boardsize,&iter);
  57.    printf("Starting test...");
  58.    for (J = 0; J < iter; J++) {
  59.       Trials = 0;
  60.       for (I =    1; I <=  Boardsize; Asub(I++) = True);
  61.       for (I =    2; I <= Boardsize+Boardsize; Bsub(I++) = True);
  62.       for (I = -(Boardsize-1); I <=  Boardsize+1; Csub(I++) = True);
  63.       Try(1,&Q);
  64.       }
  65.    printf("End of test; Trials=%ld %s solution:\n",Trials,Q?"":"no");
  66.    if (Q)
  67.       for (I = 1; I <= Boardsize;) {
  68.      printf("%4d",Xsub(I++));
  69.      }
  70.    printf("\n");
  71.    }
  72.