-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdancing_link.c
364 lines (305 loc) · 6.36 KB
/
dancing_link.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "m_mem.h"
#include "dancing_link.h"
struct head
{
struct head *right,*left,*down,*up,*c;
int name;
int num;
};
head key[1024] = {NULL};
head *array = NULL;
/*
* 1. 只是值传递从而导致了指针没有变化
* 2. 没有对初始化字符串最后的\0
* 3. 没有判断i=0的情况*/
char *m_itoa(int i,char *ch,int num)
{
char *tmp = ch + num - 1;
*tmp = '\0';
if( i == 0)
{
*(--tmp) = i + '0';
}
else
{
while(i)
{
int j = i % 10;
*(--tmp) = j + '0';
i = i / 10;
}
}
return tmp;
}
/*
* 1.关于二维数组在参数传递的时候发生的变化已经忘记
* 2.链表和数组的地址问题犯错了,以为链表的下一个和数组的下一个是一样的
* 3.if判断的时候用=替代了==这个错误很初级*/
head matrixcvtlinks(int **matrix,int row,int column)
{
int i = 0;
int j = 0;
//char str[10];
//char *strname = str;
head h = (head)MALLOC(sizeof(struct head));
array = (head*)MALLOC(sizeof(head) * column);
assert(matrix && row >= 0 && column >= 0);
assert(h);
assert(array);
h->right = h->left = h;
h->down = h->up = h->c = NULL;
for(i = 0; i < column; i++)
{
//strname = str;
head tmp1 = (head)MALLOC(sizeof(struct head));
assert(tmp1);
tmp1->right = h->left->right;
h->left->right = tmp1;
tmp1->left = h->left;
h->left = tmp1;
tmp1->down = tmp1->up = tmp1;
//strname = m_itoa(i,strname,10);
tmp1->name = i;
tmp1->num = 0;
tmp1->c = tmp1;
array[i] = tmp1;
}
for(j = 0; j < row ; j++)
{
head ptr = NULL;
for(i = 0; i < column; i++)
{
if(matrix[j][i])
{
head tmp = (head)MALLOC(sizeof(struct head));
assert(tmp);
//strname = str;
//strname = m_itoa(j,strname,10);
tmp->c = array[i];
tmp->name = j;
tmp->down = array[i]->up->down;
array[i]->up->down = tmp;
tmp->up = array[i]->up;
array[i]->up = tmp;
if(ptr == NULL)
{
tmp->right = tmp->left = tmp;
}
else
{
tmp->right = ptr->right;
ptr->right->left = tmp;
ptr->right = tmp;
tmp->left = ptr;
}
ptr = tmp;
(array[i]->num)++;
}
}
}
return h;
}
head choice_column(head h)
{
head ptr = NULL;
head tmp = h->right;
int min = 100000;
assert(h);
ptr = h->right;
for(;ptr != h;ptr=ptr->right)
{
if(min > (ptr)->num)
{
min = ptr->num;
tmp = ptr;
}
}
printf("choice name:%d\n",tmp->name);
return (tmp);
}
void cover_column(head column)
{
//删除列顶点
head ptr = column->down;
printf("cover column is %d\n",column->name);
column->left->right = column->right;
column->right->left = column->left;
//printf("name:%s,num:%d\n",column->right->name,column->right->num);
for(;ptr != column; ptr = ptr->down)
{
head ptr1 = ptr->right;
for(;ptr1 != ptr; ptr1 = ptr1->right)
{
ptr1->down->up = ptr1->up;
ptr1->up->down = ptr1->down;
ptr1->c->num--;
}
}
}
void uncover_column(head column)
{
head ptr = column->up;
assert(column);
printf("uncover column is %d\n",column->name);
for(; ptr != column; ptr = ptr->up)
{
head ptr1 = ptr->left;
for(;ptr1 != ptr; ptr1 = ptr1->left)
{
ptr1->down->up = ptr1;
ptr1->up->down = ptr1;
ptr1->c->num++;
}
}
column->right->left = column;
column->left->right = column;
}
void search(head h,int k)
{
assert(h);
if(h->right == h)
{
int i = 0;
while(key[i])
{
head tmp = key[i];
printf("key %d is : ",i);
printf("第%d行的:",tmp->name);
for(;tmp->right != key[i];tmp = tmp->right)
printf("%d ",tmp->c->name);
printf("%d ",tmp->c->name);
i++;
printf("\n");
}
display_key(4,4,5);
}
else
{
head next_column = choice_column(h);
head ptr = next_column->down;
cover_column(next_column);
for(;ptr != next_column; ptr = ptr->down)
{
printf("现在正在处理第%d行.\n",ptr->name);
head ptr1 = ptr->right;
key[k] = ptr;
for(;ptr1 != ptr; ptr1 = ptr1->right)
{
cover_column(ptr1->c);
}
printf("---------------------------------\n");
search(h,k+1);
ptr1 = key[k]->left;
next_column = key[k]->c;
for(;ptr1 != key[k];ptr1 = ptr1->left)
{
uncover_column(ptr1->c);
}
key[k] = NULL;
}
uncover_column(next_column);
#if 0
test = h->right;
for(;test != h; test = test->right)
{
printf("addr:%p\n",test);
}
#endif
}
}
void display(head h,int num)
{
int i = 0;
head ptr = array[0]->down;
head ptr1 = ptr->right;
head ptr2 = h->right;
for(;ptr2 != h; ptr2 = ptr2->right)
{
printf("2 head addr is %p\n",ptr2);
}
for(i = 0; i < num; i++)
{
printf("head addr %d is %p\n",i,array[i]);
}
for(;ptr != array[0]; ptr = ptr->down)
{
printf("c:%p,ptr:%p,array[0]:%p\n",ptr->c,ptr,array[0]);
}
for(;ptr1 != array[0]->down;ptr1 = ptr1->right)
{
printf("ptr1 addr is %p\n",ptr1);
}
}
void free_links(head h)
{
head ptr = h->right;
while(ptr != h)
{
head tmp1 = ptr;
head tmp2 = ptr->down;
ptr = ptr->right;
while(tmp2 != tmp1)
{
head tmp3 = tmp2;
tmp2 = tmp2->down;
FREE(tmp3);
}
FREE(tmp1);
}
FREE(h);
h = NULL;
FREE(array);
}
void display_key(int row,int column,int shapenum)
{
int **matrix = NULL;
int i = 0;
int j = 0;
matrix = MALLOC(sizeof(int*) * row);
for(i = 0;i < row; i++)
{
matrix[i] = MALLOC(sizeof(int) * column);
}
for(i = 0; i < row; i++)
for(j = 0; j < column; j++)
{
matrix[i][j] = 0;
}
i = 0;
while(key[i])
{
head tmp = key[i];
for(;tmp->right != key[i];tmp = tmp->right)
{
//printf("name:%d\n",tmp->c->name);
if(tmp->c->name >= shapenum)
{
int tmprow = (tmp->c->name - shapenum) / row;
int tmpcolumn = (tmp->c->name - shapenum) % row;
matrix[tmprow][tmpcolumn] = i;
}
}
if(tmp->c->name >= shapenum)
{
int tmprow = (tmp->c->name - shapenum) / row;
int tmpcolumn = (tmp->c->name - shapenum) % row;
matrix[tmprow][tmpcolumn] = i;
}
i++;
}
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
for(i = 0; i < row; i++)
FREE(matrix[i]);
FREE(matrix);
}