---
title: "880版本四阶幻方，我的机器上跑用了34ms"
author: "t0uch"
date: "2007-10-10T10:29:30Z"
tags: ["C", "C#", "C++", "J#"]
categories: ["编程语言"]
slug: "880-34ms"
draft: false
lastmod: "2007-10-10T10:29:30Z"
id: 130544
url: "https://t0uch.iteye.com/blog/130544"
---

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

001. #include &lt; iostream &gt;
002. #include &lt; time.h &gt;
003. using namespace std;
004. struct uni
005. {
006. int a, b, c, d;
007. };
008. uni can\[1032];
009. const unsigned long punk = 1UL &lt;&lt; 2;
010. int sum = 0, sum\_all = 0;
011. bool test\_4num( int a, int b, int c, int d )
012. {
013. int i;
014. for ( i = 0;i &lt;= sum;++i )
015. {
016. if ( can\[i].a == d &&
017. can\[i].b == c &&
018. can\[i].c == b &&
019. can\[i].d == a )
020. return false;
021. }
022. return true;
023. }
024. void get\_all\_num()
025. {
026. for ( int i = 1;i &lt;= 16;++i )
027. {
028. for ( int j = 1;j &lt;= 16;++j )
029. {
030. for ( int k = 1; k &lt;= 16; ++k )
031. {
032. for ( int l = 1;l &lt;= 16;++l )
033. {
034. if ( i != j && i != k && i != l && j != k && j != l && k != l && i + j + k + l == 34 && test\_4num( i, j, k, l ) )
035. {
036. can\[sum].a = i;
037. can\[sum].b = j;
038. can\[sum].c = k;
039. can\[sum].d = l;
040. ++sum;
041. }
042. }
043. }
044. }
045. }
046. //cout &lt;&lt; sum &lt;&lt; endl;
047. }
048. void p\_rint( const int sq\[] )
049. {
050. cout &lt;&lt; "i wIll gIvE yOu sOme coLoR 2CC! " &lt;&lt; sum\_all &lt;&lt; endl;
051. cout &lt;&lt; sq\[0] &lt;&lt; " " &lt;&lt; sq\[1] &lt;&lt; " " &lt;&lt; sq\[2] &lt;&lt; " " &lt;&lt; sq\[3] &lt;&lt; endl;
052. cout &lt;&lt; sq\[4] &lt;&lt; " " &lt;&lt; sq\[5] &lt;&lt; " " &lt;&lt; sq\[6] &lt;&lt; " " &lt;&lt; sq\[7] &lt;&lt; endl;
053. cout &lt;&lt; sq\[8] &lt;&lt; " " &lt;&lt; sq\[9] &lt;&lt; " " &lt;&lt; sq\[10] &lt;&lt; " " &lt;&lt; sq\[11] &lt;&lt; endl;
054. cout &lt;&lt; sq\[12] &lt;&lt; " " &lt;&lt; sq\[13] &lt;&lt; " " &lt;&lt; sq\[14] &lt;&lt; " " &lt;&lt; sq\[15] &lt;&lt; endl &lt;&lt; endl;
055. }
056. \_\_inline bool fill\_in\_line( const int c1\[], const int c2\[] )
057. {
058. int sq\[16], k, l;
059. unsigned long a, b, c, d;
060. sq\[0] = c1\[0];
061. sq\[5] = c1\[1];
062. sq\[10] = c1\[2];
063. sq\[15] = c1\[3];
064. a = punk | ( 1UL &lt;&lt; ( sq\[0] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[5] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[10] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[15] * 2 ) );
065. sq\[3] = c2\[0];
066. sq\[6] = c2\[1];
067. sq\[9] = c2\[2];
068. sq\[12] = c2\[3];
069. b = punk | ( 1UL &lt;&lt; ( sq\[3] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[6] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[9] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[12] * 2 ) );
070. if ( ( a & b ) == punk )
071. {
072. for ( k = 1;k &lt;= 16;++k )
073. {
074. sq\[4] = k;
075. sq\[7] = 34 - sq\[4] - sq\[5] - sq\[6];
076. sq\[11] = 34 - sq\[3] - sq\[7] - sq\[15];
077. sq\[8] = 34 - sq\[9] - sq\[10] - sq\[11];
078. if ( sq\[7] &gt; 0 && sq\[7] &lt;= 16 && sq\[11] &gt; 0 && sq\[11] &lt;= 16 && sq\[8] &gt; 0 && sq\[8] &lt;= 16 &&
079. sq\[4] != sq\[7] && sq\[4] != sq\[11] && sq\[4] != sq\[8] &&
080. sq\[7] != sq\[11] && sq\[7] != sq\[8] && sq\[11] != sq\[8] )
081. {
082. c = punk | ( 1UL &lt;&lt; ( sq\[4] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[7] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[11] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[8] * 2 ) );
083. if ( ( b & c ) == punk && ( a & c ) == punk )
084. {
085. for ( l = 1;l &lt;= 16;++l )
086. {
087. sq\[1] = l;
088. sq\[13] = 34 - sq\[1] - sq\[5] - sq\[9];
089. sq\[14] = 34 - sq\[12] - sq\[13] - sq\[15];
090. sq\[2] = 34 - sq\[0] - sq\[1] - sq\[3];
091. if ( sq\[13] &gt; 0 && sq\[13] &lt;= 16 && sq\[14] &gt; 0 && sq\[14] &lt;= 16 && sq\[2] &gt; 0 && sq\[2] &lt;= 16 &&
092. sq\[1] != sq\[13] && sq\[1] != sq\[14] && sq\[1] != sq\[2] &&
093. sq\[13] != sq\[14] && sq\[13] != sq\[2] && sq\[14] != sq\[2] )
094. {
095. d = punk | ( 1UL &lt;&lt; ( sq\[1] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[13] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[14] * 2 ) ) | ( 1UL &lt;&lt; ( sq\[2] * 2 ) );
096. 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 )
097. {
098. sum\_all++;
099. //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 &lt; sum;i++ )
114. {
115. for ( j = i + 1;j &lt; sum;j++ )
116. {
117. m = punk | ( 1UL &lt;&lt; ( can\[i].a * 2 ) ) | ( 1UL &lt;&lt; ( can\[i].b * 2 ) ) | ( 1UL &lt;&lt; ( can\[i].c * 2 ) ) | ( 1UL &lt;&lt; ( can\[i].d * 2 ) );
118. n = punk | ( 1UL &lt;&lt; ( can\[j].a * 2 ) ) | ( 1UL &lt;&lt; ( can\[j].b * 2 ) ) | ( 1UL &lt;&lt; ( can\[j].c * 2 ) ) | ( 1UL &lt;&lt; ( 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 &lt;&lt; "time consumed: " &lt;&lt; clock() - t &lt;&lt; " ms" &lt;&lt; endl;
141. cout &lt;&lt; sum\_all &lt;&lt; endl;
142. return 0;
143. }