[杂项项目]纯C语言实现随机生成数独九宫格

一年前的程序,可以生成一个数独的九宫格。当时想这种方法想了3天,终于实现了。由于当时刚熟悉指针,因此指针用的比较多。本人比较懒,先把代码贴出来,后序补充具体实现方法。

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>  
#include <stdlib.h>
#include <time.h>

void main()
{
int a[9][9] = { 0 };
int *p[3][3], *m[3][6], *n[3][6], *r[9][4], *aa[9][9];
int **q[9];
int i, j, k, l, h, t, s = 0;
void rank(int *x, int *y);
bool check(int a[9][9], int n);
srand((int)time(0));

for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
p[i][j] = &a[i * 3 + 1][j * 3 + 1];
for (k = 0; k < 9; k++)
q[k] = &p[k / 3][k % 3];
for (i = 0; i < 3; i++)
for (j = 0; j < 6; j++)
{
m[i][j] = &a[i * 3 + 1][2 * j - j / 2];
n[i][j] = &a[2 * j - j / 2][i * 3 + 1];
}
for (k = 0; k < 9; k++)
for (l = 0; l < 4; l++)
r[k][l] = &a[(k / 3) * 3 + (l / 2) * 2][(k % 3) * 3 + (l % 2) * 2];
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
aa[i][j] = &a[i][j];
step1:
for (i = 0; i < 9; i++)
{
next1:
**q[i] = rand() % 10;
if (**q[i] == 0)
goto next1;
for (j = 0; j < i; j++)
if (**q[j] == **q[i])
goto next1;
}
step2:
for (i = 0; i < 3; i++)
for (j = 0; j < 6; j++)
{
next2:
*m[i][j] = rand() % 10;
if (*m[i][j] == 0)
goto next2;
for (k = 0; k < 3; k++)
if (*m[i][j] == *p[i][k])
goto next2;
for (k = 0; k < j; k++)
if (*m[i][j] == *m[i][k])
goto next2;
for (k = 0; k < i; k++)
if (*m[i][j] == *m[k][j])
goto step2;
}
step3:
for (i = 0; i < 3; i++)
{
next4:
for (j = 0; j < 6; j++)
{
next3:
*n[i][j] = rand() % 10;
if (*n[i][j] == 0)
goto next3;
for (k = 0; k < 3; k++)
if (*n[i][j] == *p[k][i])
goto next3;
for (k = 0; k < j; k++)
if (*n[i][j] == *n[i][k])
goto next3;
for (l = 0; l < 2; l++)
{
if (*n[i][j] == *m[j / 2][i * 2 + l])
goto next4;
}
for (k = 0; k < i; k++)
if (*n[i][j] == *n[k][j])
goto step3;
}
}
step4:
for (i = 0; i < 3; i++)
{
for (h = 0; h < 2; h++)
{
next6:
for (l = 0; l < 3; l++)
for (j = 0; j < 2; j++)
{
next5:
*r[i * 3 + l][h * 2 + j] = rand() % 10;
if (*r[i * 3 + l][h * 2 + j] == 0)
goto next5;
if (*r[i * 3 + l][h * 2 + j] == **q[i * 3 + l])
goto next5;
for (k = 0; k < h * 2 + j; k++)
if (*r[i * 3 + l][h * 2 + j] == *r[i * 3 + l][k])
goto next5;
for (k = 0; k < 2; k++)
{
if (*r[i * 3 + l][h * 2 + j] == *m[i][l * 2 + k])
goto next5;
if (*r[i * 3 + l][h * 2 + j] == *n[l][i * 2 + k])
goto next5;
}
for (k = 0; k <= l * 3 + j * 2; k++)
for (t = 0; t < k; t++)
if (a[i * 3 + h * 2][k] == a[i * 3 + h * 2][t])
goto next6;
}
}
}
step5:
for (i = 0; i < 3; i++)
{
if (check(a, i) == false)
{
for (j = 0; j < 9; j++)
{
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
if (check(a, i) == false)
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
else if (check(a, i) == true)
break;
}
}
if (check(a, i) == false)
{
for (j = 0; j < 9; j++)
{
for (k = 8; k > j; k--)
{
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
rank(aa[k][i * 3], aa[k][i * 3 + 2]);
if (check(a, i) == false)
{
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
rank(aa[k][i * 3], aa[k][i * 3 + 2]);
}
else if (check(a, i) == true)
break;
}
if (check(a, i) == true)
break;
}
}
if (check(a, i) == false)
{
for (l = 0; l < 9; l++)
{
for (j = l + 1; j < 9; j++)
{
for (k = 8; k > j; k--)
{
rank(aa[l][i * 3], aa[l][i * 3 + 2]);
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
rank(aa[k][i * 3], aa[k][i * 3 + 2]);
if (check(a, i) == false)
{
rank(aa[l][i * 3], aa[l][i * 3 + 2]);
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
rank(aa[k][i * 3], aa[k][i * 3 + 2]);
}
else if (check(a, i) == true)
break;
}
if (check(a, i) == true)
break;
}
if (check(a, i) == true)
break;
}
}
if (check(a, i) == false)
{
for (l = 0; l < 6; l++)
{
for (h = l + 1; h < 7; h++)
{
for (j = h + 1; j < 8; j++)
{
for (k = 8; k > j; k--)
{
rank(aa[l][i * 3], aa[l][i * 3 + 2]);
rank(aa[h][i * 3], aa[h][i * 3 + 2]);
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
rank(aa[k][i * 3], aa[k][i * 3 + 2]);
if (check(a, i) == false)
{
rank(aa[l][i * 3], aa[l][i * 3 + 2]);
rank(aa[h][i * 3], aa[h][i * 3 + 2]);
rank(aa[j][i * 3], aa[j][i * 3 + 2]);
rank(aa[k][i * 3], aa[k][i * 3 + 2]);
}
else if (check(a, i) == true)
break;
}
if (check(a, i) == true)
break;
}
if (check(a, i) == true)
break;
}
if (check(a, i) == true)
break;
}
}
if (check(a, i) == false)
goto step4;
}

printf("+---------------+---------------+---------------+\n");
printf("| | | |\n");
for (i = 0; i < 9; i++)
{
printf("| ");
for (j = 0; j < 9; j++)
{
printf("%-4d", a[i][j]);
if ((j + 1) % 3 == 0)
printf("| ");
}
printf("\n");
printf("| | | |\n");
if ((i + 1) % 3 == 0)
{
printf("+---------------+---------------+---------------+\n");
if (i < 8)
printf("| | | |\n");
}
}

system("pause");
}

void rank(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}

bool check(int a[9][9], int n)
{
int i, j;
for (i = 0; i < 9; i++)
for (j = 0; j < i; j++)
if (a[i][n * 3] == a[j][n * 3])
return false;
return true;
}

纸短情长啊文章结束了但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!