Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 8
  6.  
  7. int isBoardvalid(int board[SIZE][SIZE], int Row[8]);
  8. int printArray(int array[SIZE][SIZE]);
  9. int clearColumn(int board[SIZE][SIZE], int Column);
  10. int arrayAllZero(int board[SIZE][SIZE]);
  11.  
  12. int main()
  13. {
  14. srand(time(0));
  15.  
  16. int board[SIZE][SIZE] = { 0 }; // chessboard
  17. int Row[8] = { 0 }; // each column owns a queen , array Row record which row the queen is locate in each column
  18. int count = 0; // counter for how many times the creation and validation checking of the board
  19. int Column = 0; // there are 8 columns , each column owns a queen
  20.  
  21. while (1)
  22. {
  23. arrayAllZero(board);
  24.  
  25. // create a pattern randomly
  26. for (Column = 0; Column < SIZE; Column++)
  27. {
  28. Row[Column] = rand() % 8; // assign which row the queen is locate in each column randomly
  29. }
  30.  
  31. for (Column = 0; Column < SIZE; Column++)
  32. {
  33. board[Row[Column]][Column] = 1; // assign the location of queen value 1
  34. }
  35.  
  36. count++;
  37.  
  38. if (isBoardvalid(board, Row) == 0) // the pattern is valid
  39. {
  40. printf("The %d-th solution\n", count);
  41. printArray(board);
  42. return 0;
  43. }
  44.  
  45. }
  46. return 0;
  47. }
  48.  
  49. // Find if the board valid or not , array Row record the position of queens
  50. // Return 0 if valid
  51. // Return 1 if not valid
  52. int isBoardvalid(int board[SIZE][SIZE], int Row[8])
  53. {
  54. int Column = 0;
  55. int count = 0;
  56. int i = 0;
  57.  
  58. for (Column = 0; Column <= 7; Column++) // 8-1 = 7
  59. {
  60. // board[Row[Column]][Column] is the position of queen
  61.  
  62. for (i = -7; i <= 7; i++)
  63. {
  64. if (i != 0) // if i = 0 , it's the position of queen herself
  65. {
  66. if (Column + i >= 0 && Column + i < 8 && board[Row[Column]][Column + i] != 0)
  67. {
  68. count++; // rule out same row
  69. }
  70.  
  71. if (Row[Column] + i >= 0 && Row[Column] + i < 8 && board[Row[Column] + i][Column] != 0)
  72. {
  73. count++; // rule out same column
  74. }
  75.  
  76. if (Row[Column] + i >= 0 && Row[Column] + i < 8 && Column + i >= 0 && Column + i < 8 && board[Row[Column] + i][Column + i] != 0)
  77. {
  78. count++; // rule out upper right diagonal
  79. }
  80.  
  81. if (Row[Column] + i >= 0 && Row[Column] + i < 8 && Column - i >= 0 && Column - i < 8 && board[Row[Column] + i][Column - i] != 0)
  82. {
  83. count++; // rule out lower left diagonal
  84. }
  85. }
  86. }
  87. }
  88.  
  89. if (count == 0) // no queen attacks another
  90. {
  91. return 0; // the board is valid
  92. }
  93. else
  94. {
  95. return 1; // the board is not valid
  96. }
  97. }
  98.  
  99. // Print an array , mark the posiotion of queen
  100. int printArray(int array[SIZE][SIZE])
  101. {
  102. int i = 0;
  103. int j = 0;
  104.  
  105. for (i = 0; i < SIZE; i++)
  106. {
  107. printf("-----");
  108. }
  109. printf("-\n");
  110.  
  111. for (i = 0; i < SIZE; i++)
  112. {
  113. for (j = 0; j < SIZE; j++)
  114. {
  115.  
  116. if (array[i][j] == 1)
  117. {
  118.  
  119. printf(". Q ");
  120. }
  121. else
  122. {
  123. printf(". ");
  124. }
  125. }
  126. printf(".\n");
  127. for (j = 0; j < SIZE; j++)
  128. {
  129. printf("-----");
  130. }
  131. printf("-\n");
  132. }
  133. return 0;
  134. }
  135.  
  136. // Assign 0 to all the elements of assigned column
  137. int clearColumn(int board[SIZE][SIZE], int Column)
  138. {
  139. int i = 0;
  140. int j = 0;
  141.  
  142. for (i = 0; i < SIZE; i++)
  143. {
  144. board[i][Column] = 0;
  145. }
  146. return 0;
  147. }
  148.  
  149. // Assign 0 to all the array element
  150. int arrayAllZero(int board[SIZE][SIZE])
  151. {
  152. int i = 0;
  153. int j = 0;
  154.  
  155. for (i = 0; i < SIZE; i++)
  156. {
  157. for (j = 0; j < SIZE; j++)
  158. {
  159. board[i][j] = 0;
  160. }
  161. }
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement