-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
330 lines (311 loc) · 8.76 KB
/
parser.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#define _CRT_SECURE_NO_WARNINGS 1
#include "DPLL.h"
typedef struct plaid{
int line;
int order;
int code;
}plaid;
void printToFile(int num,FILE * fp){
if(num>0)
fprintf(fp,"%d ",num-10-num/10+1);
//printf("%d ",num);
else
fprintf(fp,"%d ",-(abs(num)-10-abs(num)/10+1));
//printf("%d ",num);
}
void printClauseEnd(FILE * fp,int * clauseNum){
fprintf(fp,"0\n");
//printf("0\n");
*clauseNum+=1;
}
void limitPlaid(plaid beehive[10][10],int ord[100],FILE * fp,int * clauseNum){
int num = 1;
int line, row;
ForLine(line) {
ForRow(line, row) {
plaid nowPlaid = beehive[line][row];
if (ord[num] != 0) {
printToFile(CODE(nowPlaid.code, ord[num]), fp);
printClauseEnd(fp, clauseNum);
}
num++;
}
}
ForLine(line)
ForRow(line, row) {
plaid nowPlaid = beehive[line][row];
//此位置会填1-9
for (int i = 1; i <= 9; i++) {
printToFile(CODE(nowPlaid.code, i), fp);
}
printClauseEnd(fp, clauseNum);
//此位置不能同时填两个数
for (int i = 1; i <= 9; i++) {
for (int j = i + 1; j <= 9; j++) {
printToFile(-CODE(nowPlaid.code, i), fp);
printToFile(-CODE(nowPlaid.code, j), fp);
printClauseEnd(fp, clauseNum);
}
}
}
}
void limitLine5(plaid beehive[10],FILE * fp,int * clauseNum){
//必定会存在5
for(int i=5;i<=5;i++){
for(int j=1;j<=5;j++){
printToFile(CODE(beehive[j].code,i),fp);
}
printClauseEnd(fp,clauseNum);
}
//选填情况
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
for(int k=1;k<=5;k++){
printToFile(CODE(beehive[k].code,i),fp);
printToFile(CODE(beehive[k].code,j+5),fp);
}
printClauseEnd(fp,clauseNum);
}
}
//每两格不同时填一个数
for(int i=1;i<=9;i++){
for(int j=1;j<5;j++){
for(int k=j+1;k<=5;k++){
printToFile(-CODE(beehive[j].code,i),fp);
printToFile(-CODE(beehive[k].code,i),fp);
printClauseEnd(fp,clauseNum);
}
}
}
}
void limitLine6(plaid beehive[10],FILE * fp,int * clauseNum){
//必定会存在4,5,6
for(int i=4;i<=6;i++){
for(int j=1;j<=6;j++){
printToFile(CODE(beehive[j].code,i),fp);
}
printClauseEnd(fp,clauseNum);
}
//选填情况
for(int i=1;i<=3;i++){
for(int j=1;j<=i;j++){
for(int k=1;k<=6;k++){
printToFile(CODE(beehive[k].code,i),fp);
printToFile(CODE(beehive[k].code,j+6),fp);
}
printClauseEnd(fp,clauseNum);
}
}
//每两格填不同的数
for(int i=1;i<=9;i++){
for(int j=1;j<6;j++){
for(int k=j+1;k<=6;k++){
printToFile(-CODE(beehive[j].code,i),fp);
printToFile(-CODE(beehive[k].code,i),fp);
printClauseEnd(fp,clauseNum);
}
}
}
}
void draw(plaid beehive[10][10]){
int line,row;
ForLine(line){
int num = 9 - abs(5-line);
for(int i=1;i<=9-num;i++){
printf(" ");
}
ForRow(line,row){
printf("%2d ",beehive[line][row].code);
}
printf("\n");
}
}
void limitLine7(plaid beehive[10],FILE * fp,int * clauseNum){
//必定会有3,4,5,6,7
for(int i=3;i<=7;i++){
for(int j=1;j<=7;j++){
printToFile(CODE(beehive[j].code,i),fp);
}
printClauseEnd(fp,clauseNum);
}
//选填情况
for(int i=1;i<=7;i++){
printToFile(CODE(beehive[i].code,1),fp);
printToFile(CODE(beehive[i].code,8),fp);
}
printClauseEnd(fp,clauseNum);
for(int i=1;i<=7;i++){
printToFile(CODE(beehive[i].code,2),fp);
printToFile(CODE(beehive[i].code,8),fp);
}
printClauseEnd(fp,clauseNum);
for(int i=1;i<=7;i++){
printToFile(CODE(beehive[i].code,2),fp);
printToFile(CODE(beehive[i].code,9),fp);
}
printClauseEnd(fp,clauseNum);
//每两格填不同的数
for(int i=1;i<=9;i++){
for(int j=1;j<7;j++){
for(int k=j+1;k<=7;k++){
printToFile(-CODE(beehive[j].code,i),fp);
printToFile(-CODE(beehive[k].code,i),fp);
printClauseEnd(fp,clauseNum);
}
}
}
}
void limitLine8(plaid beehive[10],FILE * fp,int * clauseNum){
//2-8是必存在的
for(int i=2;i<=8;i++){
for(int j=1;j<=8;j++){
printToFile(CODE(beehive[j].code,i),fp);
}
printClauseEnd(fp,clauseNum);
}
//选填情况
for(int i=1;i<=8;i++){
printToFile(CODE(beehive[i].code,1),fp);
printToFile(CODE(beehive[i].code,9),fp);
}
printClauseEnd(fp,clauseNum);
//每两格不同时为一个数
for(int i=1;i<=9;i++){
for(int j=1;j<8;j++){
for(int k=j+1;k<=8;k++){
printToFile(-CODE(beehive[j].code,i),fp);
printToFile(-CODE(beehive[k].code,i),fp);
printClauseEnd(fp,clauseNum);
}
}
}
}
void limitLine9(plaid beehive[10],FILE * fp,int * clauseNum){
//所有数都必存在
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
printToFile(CODE(beehive[j].code,i),fp);
}
printClauseEnd(fp,clauseNum);
}
//每两格不同时为一个数
for(int i=1;i<=9;i++){
for(int j=1;j<9;j++){
for(int k=j+1;k<=9;k++){
printToFile(-CODE(beehive[j].code,i),fp);
printToFile(-CODE(beehive[k].code,i),fp);
printClauseEnd(fp,clauseNum);
}
}
}
}
//将蜂窝顺时针转60度
void convertBeehive(plaid beehive[10][10]){
plaid newBeehive[10][10];
for(int i=1,j=5;i<=5;i++,j--){
newBeehive[1][i]=beehive[j][1];
}
newBeehive[2][1]=beehive[6][1];
for(int i=2,j=5;i<=6;i++,j--){
newBeehive[2][i]=beehive[j][2];
}
newBeehive[3][1]=beehive[7][1];
newBeehive[3][2]=beehive[6][2];
for(int i=3,j=5;i<=7;i++,j--){
newBeehive[3][i]=beehive[j][3];
}
newBeehive[4][1]=beehive[8][1];
newBeehive[4][2]=beehive[7][2];
newBeehive[4][3]=beehive[6][3];
for(int i=4,j=5;i<=8;i++,j--){
newBeehive[4][i]=beehive[j][4];
}
newBeehive[5][1]=beehive[9][1];
newBeehive[5][2]=beehive[8][2];
newBeehive[5][3]=beehive[7][3];
newBeehive[5][4]=beehive[6][4];
for(int i=5,j=5;i<=9;i++,j--){
newBeehive[5][i]=beehive[j][5];
}
for(int i=1,j=9;i<=5;i++,j--){
newBeehive[6][i]=beehive[j][i+1];
}
newBeehive[6][6]=beehive[4][6];
newBeehive[6][7]=beehive[3][6];
newBeehive[6][8]=beehive[2][6];
for(int i=1,j=9;i<=5;i++,j--){
newBeehive[7][i]=beehive[j][i+2];
}
newBeehive[7][6]=beehive[4][7];
newBeehive[7][7]=beehive[3][7];
for(int i=1,j=9;i<=5;i++,j--){
newBeehive[8][i]=beehive[j][i+3];
}
newBeehive[8][6]=beehive[4][8];
for(int i=1,j=9;i<=5;i++,j--){
newBeehive[9][i]=beehive[j][i+4];
}
int line,row;
ForLine(line){
ForRow(line,row){
beehive[line][row]=newBeehive[line][row];
}
}
}
void limitLine(plaid beehive[10][10],FILE * fp,int * clauseNum){
int line;
ForLine(line){
int num=9-abs(5-line);
switch (num)
{
case 5:
limitLine5(beehive[line],fp,clauseNum);
break;
case 6:
limitLine6(beehive[line],fp,clauseNum);
break;
case 7:
limitLine7(beehive[line],fp,clauseNum);
break;
case 8:
limitLine8(beehive[line],fp,clauseNum);
break;
case 9:
limitLine9(beehive[line],fp,clauseNum);
break;
default:
break;
}
}
}
void creatBeehive(plaid beehive[10][10]){
int code=1,line,row;
ForLine(line){
ForRow(line,row){
beehive[line][row].code=code;
code++;
}
}
}
void cnfParser(int level,char FileName[100],int ord[100]) {
FILE * tempFP = fopen("temp.cnf","w");
plaid beehive[10][10];
creatBeehive(beehive);
int clauseNum = 0;
limitPlaid(beehive,ord,tempFP,&clauseNum);
for(int i=1;i<=3;i++){
draw(beehive);
limitLine(beehive,tempFP,&clauseNum);
convertBeehive(beehive);
}
fclose(tempFP);
tempFP = fopen("temp.cnf","r");
FILE * cnfFP = fopen(FileName,"w");
fprintf(cnfFP,"p cnf %d %d\n",549,clauseNum);
char ords[500];
while(fgets(ords,499,tempFP)!=NULL){
fprintf(cnfFP,"%s",ords);
}
fclose(tempFP);
fclose(cnfFP);
}