Я не могу понять код, который мне нужно будет уже послезавтра объяснить. Задача состоит в том, чтобы найти количество решений для латинского квадрата и вывести эти квадраты на экран:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
| int Factorial(int n)
{if (n<=1) return 1;
else return n*Factorial(n-1);
}
const int N = 4;
const int Nf = Factorial(N);
struct strN
{ int mas_[N];
strN(){}
strN (int * ptr)
{ for(int i = 0; i<N; i++) mas_[i]=ptr[i];
}
};
struct AS
{ strN* mas;
int count;
AS() { mas = new strN[Nf];
count = 0;
}
AS& operator += (strN str)
{ mas[count] = str;
count++;
return *this;
}
} ArrayStr;
int i=0;
int k=0;
int P1[N];
bool Next1()
{ if (i == 0) { P1[0]=1; i++; return true;}
if (i == N) return false;
int j ;
for ( j = 0; j<N; j++)
{
int j1;
for ( j1 = 0; j1<i; j1++)
if ( P1[j1] == (j+1) ) break;
if(j1 == i) break;
}
if( j== N ) return false;
P1[i]=j+1;
i++;
return true;
}
bool Back1()
{ i--;
if (i==0) return false;
int i1 = P1[i-1];
int j = i1 ;
for ( j = i1; j<N; j++)
{
int j1;
for ( j1 = 0; j1<i; j1++) if (P1[j1] == (j+1)) break;
if(j1 == i) break;
}
if( j<N )
{ P1[i-1] = j+1;
return true;
}
return Back1();
}
void Test1()
{ bool t2 =true;
bool t1;
while (t2)
{ t1 =true;
while(t1) t1 = Next1();
//for(int j = 0; j<i; j++) printf("%d ", P1[j]); printf("\n");
ArrayStr += P1;
t2=Back1();
}
for(int j = 0; j<ArrayStr.count; j++)
{ printf("%2d) ", j);
for(int j1 = 0; j1<N; j1++) printf("%d ", ArrayStr.mas[j].mas_[j1]);
printf("\n");
}
}
strN* P2 = new strN[N];
int* strNum = new int[N];
bool Next2()
{ if ( i==0 ) {P2[i] = ArrayStr.mas[0]; strNum[i]=0; i++; return true; }
if ( i== N ) return false;
int j = 0;
int j1 = 0;
bool t = false;
strN newStr ;
for( j=0; j<Nf; j++)
{ newStr = ArrayStr.mas[j];
t =true;
for( j1=0; j1<i; j1++)
{ for( int j2=0; j2<N; j2++)
t= t && ( newStr.mas_[j2] != P2[j1].mas_[j2]);
if(!t) break;
}
if (t) break;
}
if (t) { P2[i] = newStr;
strNum[i] = j;
i++;
return true;
}
return false;
}
bool Back2()
{
i--;
if (i==0) return false;
strN newStr;
int j, j1 ;
bool t =false ;
for( j= strNum[i-1]+1; j<Nf; j++)
{ newStr = ArrayStr.mas[j];
t = false;
for( j1=0; j1<i-1; j1++)
if (j == strNum[j1]) continue;
t = true;
for( j1=0; j1<i-1; j1++)
{ for( int j2=0; j2<N; j2++)
t= t && ( newStr.mas_[j2] != P2[j1].mas_[j2]) ;
if( !t) break;
}
if (t) break;
}
if(t && j<Nf)
{ P2[i-1] = newStr;
strNum[i-1]=j;
return true;
}
return Back2();
}
bool CheckDiag()
{ int nn1 = 1;
int nn2 = 1;
for(int j = 0; j<N; j++)
{ nn1=nn1*P2[j].mas_[j] ;
nn2=nn2*P2[j].mas_[N-1-j] ;
}
if (nn1 != Nf )return false;
if (nn2 != Nf )return false;
return true;
}
void PrintM()
{ printf("\n");
for(int j = 0; j<N; j++)
{ for(int j1 = 0; j1<N; j1++)
printf("%d ", P2[j].mas_[j1]);
printf(" (%d)\n", strNum[j]);
}
}
void Test()
{ Test1();
printf("\n\n");
i=0;
k=0;
int kk =0;
bool t1;
bool t2 = true;
while (t2)
{ t1 = true;
while (t1) t1 = Next2();
kk++;
bool tt = CheckDiag();
if ( CheckDiag())
{ k++;
if (k<40) PrintM() ;
}
t2 = Back2();
}
printf ("\n\n kk=%d k=%d \n", kk, k);
} |
|