ITeye博客

880版本四阶幻方,我的机器上跑用了34ms

t0uch · 2007-10-10 10:29 · C · 编程语言 · C · C++ · J# · C# · Markdown

闲来无事,改进改进,多说无益,贴代码,看帖要回帖啊各位

  1. #include < iostream >
  2. #include < time.h >
  3. using namespace std;
  4. struct uni
  5. {
  6. int a, b, c, d;
  7. };
  8. uni can[1032];
  9. const unsigned long punk = 1UL << 2;
  10. int sum = 0, sum_all = 0;
  11. bool test_4num( int a, int b, int c, int d )
  12. {
  13. int i;
  14. for ( i = 0;i <= sum;++i )
  15. {
  16. if ( can[i].a == d &&
  17. can[i].b == c &&
  18. can[i].c == b &&
  19. can[i].d == a )
  20. return false;
  21. }
  22. return true;
  23. }
  24. void get_all_num()
  25. {
  26. for ( int i = 1;i <= 16;++i )
  27. {
  28. for ( int j = 1;j <= 16;++j )
  29. {
  30. for ( int k = 1; k <= 16; ++k )
  31. {
  32. for ( int l = 1;l <= 16;++l )
  33. {
  34. if ( i != j && i != k && i != l && j != k && j != l && k != l && i + j + k + l == 34 && test_4num( i, j, k, l ) )
  35. {
  36. can[sum].a = i;
  37. can[sum].b = j;
  38. can[sum].c = k;
  39. can[sum].d = l;
  40. ++sum;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. //cout << sum << endl;
  47. }
  48. void p_rint( const int sq[] )
  49. {
  50. cout << "i wIll gIvE yOu sOme coLoR 2CC! " << sum_all << endl;
  51. cout << sq[0] << " " << sq[1] << " " << sq[2] << " " << sq[3] << endl;
  52. cout << sq[4] << " " << sq[5] << " " << sq[6] << " " << sq[7] << endl;
  53. cout << sq[8] << " " << sq[9] << " " << sq[10] << " " << sq[11] << endl;
  54. cout << sq[12] << " " << sq[13] << " " << sq[14] << " " << sq[15] << endl << endl;
  55. }
  56. __inline bool fill_in_line( const int c1[], const int c2[] )
  57. {
  58. int sq[16], k, l;
  59. unsigned long a, b, c, d;
  60. sq[0] = c1[0];
  61. sq[5] = c1[1];
  62. sq[10] = c1[2];
  63. sq[15] = c1[3];
  64. a = punk | ( 1UL << ( sq[0] * 2 ) ) | ( 1UL << ( sq[5] * 2 ) ) | ( 1UL << ( sq[10] * 2 ) ) | ( 1UL << ( sq[15] * 2 ) );
  65. sq[3] = c2[0];
  66. sq[6] = c2[1];
  67. sq[9] = c2[2];
  68. sq[12] = c2[3];
  69. b = punk | ( 1UL << ( sq[3] * 2 ) ) | ( 1UL << ( sq[6] * 2 ) ) | ( 1UL << ( sq[9] * 2 ) ) | ( 1UL << ( sq[12] * 2 ) );
  70. if ( ( a & b ) == punk )
  71. {
  72. for ( k = 1;k <= 16;++k )
  73. {
  74. sq[4] = k;
  75. sq[7] = 34 - sq[4] - sq[5] - sq[6];
  76. sq[11] = 34 - sq[3] - sq[7] - sq[15];
  77. sq[8] = 34 - sq[9] - sq[10] - sq[11];
  78. if ( sq[7] > 0 && sq[7] <= 16 && sq[11] > 0 && sq[11] <= 16 && sq[8] > 0 && sq[8] <= 16 &&
  79. sq[4] != sq[7] && sq[4] != sq[11] && sq[4] != sq[8] &&
  80. sq[7] != sq[11] && sq[7] != sq[8] && sq[11] != sq[8] )
  81. {
  82. c = punk | ( 1UL << ( sq[4] * 2 ) ) | ( 1UL << ( sq[7] * 2 ) ) | ( 1UL << ( sq[11] * 2 ) ) | ( 1UL << ( sq[8] * 2 ) );
  83. if ( ( b & c ) == punk && ( a & c ) == punk )
  84. {
  85. for ( l = 1;l <= 16;++l )
  86. {
  87. sq[1] = l;
  88. sq[13] = 34 - sq[1] - sq[5] - sq[9];
  89. sq[14] = 34 - sq[12] - sq[13] - sq[15];
  90. sq[2] = 34 - sq[0] - sq[1] - sq[3];
  91. if ( sq[13] > 0 && sq[13] <= 16 && sq[14] > 0 && sq[14] <= 16 && sq[2] > 0 && sq[2] <= 16 &&
  92. sq[1] != sq[13] && sq[1] != sq[14] && sq[1] != sq[2] &&
  93. sq[13] != sq[14] && sq[13] != sq[2] && sq[14] != sq[2] )
  94. {
  95. d = punk | ( 1UL << ( sq[1] * 2 ) ) | ( 1UL << ( sq[13] * 2 ) ) | ( 1UL << ( sq[14] * 2 ) ) | ( 1UL << ( sq[2] * 2 ) );
  96. if ( ( a & d ) == punk && ( b & d ) == punk && ( c & d ) == punk && sq[0] + sq[4] + sq[8] + sq[12] == 34 && sq[2] + sq[6] + sq[10] + sq[14] == 34 )
  97. {
  98. sum_all++;
  99. //p_rint(sq);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. return true;
  108. }
  109. void fill_in_eight()
  110. {
  111. int cse1[4], cse2[4], i, j;
  112. unsigned long m, n;
  113. for ( i = 0;i < sum;i++ )
  114. {
  115. for ( j = i + 1;j < sum;j++ )
  116. {
  117. m = punk | ( 1UL << ( can[i].a * 2 ) ) | ( 1UL << ( can[i].b * 2 ) ) | ( 1UL << ( can[i].c * 2 ) ) | ( 1UL << ( can[i].d * 2 ) );
  118. n = punk | ( 1UL << ( can[j].a * 2 ) ) | ( 1UL << ( can[j].b * 2 ) ) | ( 1UL << ( can[j].c * 2 ) ) | ( 1UL << ( can[j].d * 2 ) );
  119. if ( ( m & n ) == punk )
  120. {
  121. cse1[0] = can[i].a;
  122. cse1[1] = can[i].b;
  123. cse1[2] = can[i].c;
  124. cse1[3] = can[i].d;
  125. cse2[0] = can[j].a;
  126. cse2[1] = can[j].b;
  127. cse2[2] = can[j].c;
  128. cse2[3] = can[j].d;
  129. fill_in_line( cse1, cse2 );
  130. }
  131. }
  132. }
  133. }
  134. int main()
  135. {
  136. time_t t;
  137. get_all_num();
  138. t = clock();
  139. fill_in_eight();
  140. cout << "time consumed: " << clock() - t << " ms" << endl;
  141. cout << sum_all << endl;
  142. return 0;
  143. }

评论(2)

  1. #2 楼 · Sing · 2008-02-20 17:02

    我的机器上大约需要200ms左右,改进的效率还是比较高的

  2. #1 楼 · stephen · 2007-10-10 13:32

    不如说明一下你的这个程序在同类程序中算是好的还是坏的?如果是好的,好在什么方面?你这样放代码出来,估计没什么人有兴趣看,除了准备抄作业的之外,:)

评论功能维护中,当前仅展示历史评论。