Eight queens Problem (n queens Problem): recursive and non-recursive solutions (C ++)

Source: Internet
Author: User

Eight queens Problem: put eight queens on the 8*8 lattice board. Any two queens cannot be in the same row, in the same column, or on the same diagonal line. There are several ways to solve this problem.

Queen N problem: Put N queens on the board of N * n grids. Any two queens cannot be in the same row, in the same column, or on the same diagonal line. There are several methods to solve this problem.

 

  1. # Include "stdafx. H"
  2. # Include <iostream>
  3. # Include <fstream>
  4. Using namespace STD;
  5. // Non-recursion
  6. Void Queen (int n)
  7. {
  8. Int * c = new int [N]; // records the column status
  9. Int * D1 = new int [2 * n-1]; // records the positive/diagonal state.
  10. Int * D2 = new int [2 * n-1]; // records the negative diagonal state.
  11. Int * lc = new int [N]; // records the number of columns placed by the Queen on the nth node.
  12. Int ** Queen = new int * [n]; // chessboard
  13. For (int I = 0; I <n; I ++) // Initialization
  14. {
  15. Queen [I] = new int [n];
  16. For (int j = 0; j <n; j ++)
  17. {
  18. Queen [I] [j] = 0;
  19. }
  20. C [I] = 0;
  21. D1 [I] = 0;
  22. D1 [2 * n-i-2] = 0;
  23. D2 [I] = 0;
  24. D2 [2 * n-i-2] = 0;
  25. }
  26. Int line = 0; // line
  27. Int column = 0; // column
  28. _ Int64 num = 0; // number of items that can be placed
  29. _ Int64 count = 0; // number of cycles
  30. While (line> = 0)
  31. {
  32. While (column <n)
  33. {
  34. Count ++;
  35. If (c [column] = 0 & d1 [line-column + n-1] = 0 & d2 [line + column] = 0) // if a column, true if no plus or minus diagonal lines are placed
  36. {
  37. If (line = N-1) // if it reaches the last line, it is true
  38. {
  39. Queen [Line] [column] = 1;
  40. Num ++; // Add 1 to the placement quantity
  41. Queen [Line] [column] = 0;
  42. Break;
  43. }
  44. Else
  45. {
  46. // Record the placement status
  47. Queen [Line] [column] = 1;
  48. C [column] = 1;
  49. D1 [Line-column + n-1] = 1;
  50. D2 [LINE + column] = 1;
  51. LC [Line] = column;
  52. Line ++; // enter the next row, starting from column 0th
  53. Column = 0;
  54. }
  55. }
  56. Else
  57. Column ++;
  58. }
  59. Count ++;
  60. // If all cells in this row cannot be placed, move the row back and traverse the row starting from the next column placed in the previous row.
  61. Line --;
  62. If (line> = 0)
  63. {
  64. Column = Lc [Line];
  65. Queen [Line] [column] = 0;
  66. C [column] = 0;
  67. D1 [Line-column + n-1] = 0;
  68. D2 [line + column] = 0;
  69. Column ++;
  70. }
  71. }
  72. Cout <num <'/t' <count <endl ;;
  73. }
  74. // Recursion
  75. Void queen1 (int I, int ** Queen, int * a, int * d1, int * d2, int & n, int & QueenNumber ,__ int64 & count)
  76. {
  77. Int iColumn;
  78. For (iColumn = 0; iColumn <n; iColumn ++)
  79. {
  80. Count ++;
  81. If (a [iColumn] = 0 & d1 [I-iColumn + n-1] = 0 & d2 [I + iColumn] = 0)
  82. {
  83. Queen [I] [iColumn] = 1;
  84. A [iColumn] = 1;
  85. D1 [I-iColumn + n-1] = 1;
  86. D2 [I + iColumn] = 1;
  87. If (I <n-1)
  88. Queen1 (I + 1, Queen, a, d1, d2, n, QueenNumber, count );
  89. Else
  90. {
  91. QueenNumber ++;
  92. /*
  93. Fstream outstuf;
  94. Outstuf. open ("out.txt", ios: out | ios: app );
  95. For (int I = 0; I <8; I ++)
  96. {
  97. For (int j = 0; j <8; j ++)
  98. {
  99. Outstuf <Queen [I] [j] <"";
  100. }
  101. Outstuf <"/r/n ";
  102. }
  103. Outstuf <"/r/n ";
  104. Outstuf. close ();*/
  105. }
  106. Queen [I] [iColumn] = 0;
  107. A [iColumn] = 0;
  108. D1 [I-iColumn + n-1] = 0;
  109. D2 [I + iColumn] = 0;
  110. }
  111. }
  112. }
  113. Void queendg (int n)
  114. {
  115. Int * c = new int [n];
  116. Int * d1 = new int [2 * n-1];
  117. Int * d2 = new int [2 * n-1];
  118. Int ** Queen = new int * [n];
  119. For (int I = 0; I <n; I ++)
  120. {
  121. Queen [I] = new int [n];
  122. For (int j = 0; j <n; j ++)
  123. {
  124. Queen [I] [j] = 0;
  125. }
  126. C [I] = 0;
  127. D1 [I] = 0;
  128. D1 [2 * n-i-2] = 0;
  129. D2 [I] = 0;
  130. D2 [2 * n-i-2] = 0;
  131. }
  132. Int QueenNumber = 0;
  133. _ Int64 count = 0;
  134. Queen1 (0, Queen, c, d1, d2, n, QueenNumber, count );
  135. Cout <QueenNumber <'/t' <count <endl ;;
  136. }
  137. Int _ tmain (int argc, _ TCHAR * argv [])
  138. {
  139. Queendg (8 );
  140. Queen (8 );
  141. Return 0;
  142. }

Tested

There are 14772512 methods for putting Queen (16) in total, in 206 seconds

A total of 14772512 queendg (16) release methods, with a total time of 248 seconds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.