The sign triangle problem of backtracking method

Source: Internet
Author: User

  1. /* Backtracking to solve symbolic triangle problems
  2. Problem Description:
  3. As a symbolic triangle consisting of 14 "+" and 14 "-", 2 are "+" under the same number, and 2 are "-" below.
  4. - + + - + + +
  5. - + - - + +
  6. - - + - +
  7. + - - -
  8. - + +
  9. - +
  10. -
  11. In general, the first line of the symbol triangle has n symbols, and the sign triangle problem requires a given n,
  12. Calculates how many different symbol triangles have the same number of "+" and "-" contained.
  13. Problem Solving Ideas:
  14. 1, constantly change the first line of each symbol, search for a solution that meets the criteria, you can use recursive backtracking
  15. For ease of operation, set + to 0,-to 1, which allows you to use an XOR operator to represent the relationship of the symbol triangle
  16. + + is 0^0=0,--for + that is 1^1=0, +--that is, 0^1=1,-+-that is, 1^0=1;
  17. 2, because the number of two symbols is the same, you can prune the tree,
  18. When the total number of symbols is odd, there is no solution when a symbol exceeds half of the total
  19. Refer to the learning materials, re-implementation to practice, there are omissions please correct [email protected].
  20. Yang Xiaojin, 17:13 2009-8-5
  21. */
  22. #include "iostream"
  23. Using namespace std;
  24. typedef unsigned char uchar;
  25. Char cc[2]={' + ','-'}; //Easy to output
  26. int n, //Total number of first line symbols
  27. Half, //Total symbols in half
  28. Counter //1 count, i.e. "-" number count
  29. Uchar **p; //Symbol storage space
  30. Long sum; //Meet the criteria for the triangle Count
  31. T, the first line of the T-symbol
  32. void BackTrace (int t)
  33. {
  34. int I, J;
  35. if (T > N)
  36. {//symbol fills complete
  37. sum++;
  38. //Print symbols
  39. cout << "<< sum << " A:/n ";
  40. For (i=1; i<=n; ++i)
  41. {
  42. For (j=1; j<i; ++j)
  43. {
  44. cout << "";
  45. }
  46. For (j=1; j<=n-i+1; ++j)
  47. {
  48. cout << cc[P[i][j]] << "";
  49. }
  50. cout << "/n";
  51. }
  52. }
  53. Else
  54. {
  55. For (i=0; i<2; ++i)
  56. {
  57. P[1][t] = i; //First line of T-symbol
  58. Counter + = i; //"-" number statistics
  59. for (j=2; j<=t; ++j) //When the first line of the symbol >=2, you can calculate some of the symbols in the following line
  60. {
  61. P[J][T-J+1] = p[j-1][t-j+1]^p[j-1][t-j+2]; //Via XOR or op-downlink symbol
  62. Counter + = p[j][t-j+1];
  63. }
  64. if ((counter <= half) && (t* (t+1)/2-counter <= half))
  65. {//If the symbol statistics are not more than half, and the other symbol is not more than half
  66. BackTrace (t+1); //Add the next symbol in the first line
  67. }
  68. //backtracking, judging another symbol situation
  69. For (j=2; j<=t; ++j)
  70. {
  71. Counter-= p[j][t-j+1];
  72. }
  73. Counter-= i;
  74. }
  75. }
  76. }
  77. int main ()
  78. {
  79. cout << "Please enter the first line of the number of symbols N:";
  80. CIN >> N;
  81. Counter = 0;
  82. sum = 0;
  83. Half = N (n+1)/2;
  84. int i=0;
  85. if (half%2 = = 0)
  86. {//Total number must be even, if odd, no solution
  87. Half/= 2;
  88. p = new Uchar *[n+1];
  89. For (i=0; i<=n; ++i)
  90. {
  91. P[i] = new uchar[n+1];
  92. memset (P[i], 0, sizeof (UCHAR) * (n+1));
  93. }
  94. BackTrace (1);
  95. For (i=0; i<=n; ++i)
  96. {
  97. delete[] p[i];
  98. }
  99. delete[] p;
  100. }
  101. cout << "/n Total" << sum << "x" << Endl;
  102. return 0;
  103. }

The sign triangle problem of backtracking method

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.