-
Notifications
You must be signed in to change notification settings - Fork 1
/
A Number Link Game.cpp
538 lines (465 loc) · 9.87 KB
/
A Number Link Game.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* Description: Code printing valid tables for the game of flow.
Creator: Vaibhav Agarwal (vaiagarwal96@iitkgp.ac.in) */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct _node
{
struct _node *parent;
int rank;
int path_number;
int endpoint;
};
typedef struct _node node;
/* Name: initboard()
Input: 2D-array of pointers, size of array row/column
Output: --void--
Description: Takes a table of pointers and initializes it. */
void initboard(node ***arr, int n)
{
int i, j;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
node *np;
np = (node *)malloc(sizeof(node));
np->rank = 0;
np->parent = NULL;
np->path_number = 0;
np->endpoint = 0;
arr[i][j] = np;
}
}
}
/***********************/
/* Name: findset()
Input: a node
Output: the set pointer of the set the node belongs to
Description: Takes a node and returns the set pointer. */
node *findset(node *n)
{
if (n->parent != NULL)
n = n->parent;
return n;
}
/************************/
/* Name: setunion()
Input: nodes x and y
Output: --void--
Description: merges the two sets containing x and y.
*/
void setunion(node *x, node *y)
{
x = findset(x);
y = findset(y);
if (x->rank > y->rank)
y->parent = x;
else{
x->parent = y;
if(x->rank == y->rank)
y->rank++;
}
}
/**************************/
/* Name: neighbour()
Input: size of array row/column, 2D-array of pointers
Output: returns the incripted values of k1 and k2 or -1 if fails.
Description: Finds the valid starting points of the flow. */
int neighbour(int n, node ***arr)
{
int i1, i2, j1, j2, ct = 0, flag = 0, a, b,k2;
int k = rand()%(n*n);
while (ct < (n*n)){
k %= (n*n);
i1 = k/n;
j1 = k%n;
if (arr[i1][j1]->path_number==0)
{
int kk = rand()%4;
int cc = 0;
switch (kk)
{
case 0: i2= i1-1;
j2= j1-0;
if(i2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 1: i2= i1-0;
j2= j1-1;
if(j2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 2: i2= i1+1;
j2= j1-0;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 3: i2= i1-0;
j2= j1+1;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 4: if(cc==4)
break;
i2= i1-1;
j2= j1-0;
if(i2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 5: if(cc==4)
break;
i2= i1-0;
j2= j1-1;
if(j2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 6: if(cc==4)
break;
i2= i1+1;
j2= j1-0;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
case 7: if(cc==4)
break;
i2= i1-0;
j2= j1+1;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
flag=1;
break;
}
}
cc++;
}
}
if(flag==1)
break;
ct++;
k++;
}
if(ct<n*n){
k2= (i2*n)+j2;
return k*(n*n)+k2;
}
else{
return -1;
}
}
/************************/
/* Name: checkneigh()
Input: k1, k2, size of array row/column, 2D-array of pointers
Output: returns 1 if true, 0 if fails.
Description: Checks the neighbouring 4 vaues and find if there are more than 1 neighbours of the same set.
*/
int checkneigh(int k1, int k2, int n, node ***arr)
{
int i= k2/n;
int j= k2%n;
int ii= k1/n;
int jj= k1%n;
int ct=0;
if(i>0 && findset(arr[i-1][j])==findset(arr[ii][jj]))
ct++;
if(i<n-1 && findset(arr[i+1][j])==findset(arr[ii][jj]))
ct++;
if(j>0 && findset(arr[i][j-1])==findset(arr[ii][jj]))
ct++;
if(j<n-1 && findset(arr[i][j+1])==findset(arr[ii][jj]))
ct++;
if(ct>1)
return 0;
else
return 1;
}
/****************************/
/* Name: valid_next()
Input: starting k, size of array row/column, 2D-array of pointers
Output: returns the value of k2 or -1 if fails.
Description: Finds the next valid points of the flow.
*/
int valid_next(int k, int n, node ***arr)
{
int i1, i2, j1, j2, a, b, kk, stat,ct=0;
int flag=0;
i1= k/n;
j1= k%n;
kk= rand()%4;
switch(kk)
{
case 0: i2= i1-1;
j2= j1-0;
if(i2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 1: i2= i1-0;
j2= j1-1;
if(j2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 2: i2= i1+1;
j2= j1-0;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 3: i2= i1-0;
j2= j1+1;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 4: if(ct==4)
break;
i2= i1-1;
j2= j1-0;
if(i2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 5: if(ct==4)
break;
i2= i1-0;
j2= j1-1;
if(j2>=0 && i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 6: if(ct==4)
break;
i2= i1+1;
j2= j1-0;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
case 7: if(ct==4)
break;
i2= i1-0;
j2= j1+1;
if(i2<n && j2<n){
if(arr[i2][j2]->path_number==0){
stat= checkneigh(k, (n*i2 + j2),n,arr);
//printf("%d\n",stat);
if(stat){
flag=1;
break;
}
}
}
ct++;
}
//printf("flag- %d\n",flag);
if(flag==0)
return -1;
if(flag){
//printf("value sent- %d\n", i2*n + j2);
return (i2*n)+j2;
}
}
/********************/
/* Name: addpath()
Input: 2D-array of pointers, size of array row/column, path number
Output: returns 1 if a new path can be (or, is) added, 0 if not.
Description: Creates a new flow path.
*/
int addpath(node ***arr, int n, int ptno)
{
int a,b,k1,k2;
int i1,j1,i2,j2;
k2= neighbour( n, arr);
if(k2==-1) //no valid pair found to start with
return 0;
k1= k2/(n*n);
k2= k2%(n*n);
//printf("%d %d\n",k1,k2);
i1= k1/n;
j1= k1%n;
i2= k2/n;
j2= k2%n;
//printf("%d %d\n",i1,j1);
//printf("%d %d\n",i2,j2);
//found i1,j1 and i2,j2
arr[i1][j1]->endpoint= 1;
arr[i2][j2]->path_number= ptno;
arr[i1][j1]->path_number= ptno;
node *n1, *n2;
n1= arr[i1][j1];
n2= arr[i2][j2];
n1= findset(n1);
n2= findset(n2);
setunion(n1, n2);
while(1){
i1= i2;
j1= j2;
k1= (i1*n)+j1;
/******/
k2= valid_next(k1,n,arr);
//printf("k2- %d\n",k2);
if(k2==-1){
arr[i1][j1]->endpoint= 1;
break;
}
i2=k2/n;
j2=k2%n;
//printf("%d %d\n",i2,j2);
arr[i2][j2]->path_number= ptno;
node *n1, *n2;
n1= arr[i1][j1];
n2= arr[i2][j2];
n1= findset(n1);
n2= findset(n2);
setunion(n1,n2);
}
return 1;
}
/************************/
/* Name: printtable()
Input: 2D-array of pointers, size of array row/column
Output: --void--
Description: Prints the question table and the corresponding solution table.
*/
void printtable(node ***arr, int n)
{
int i,j;
printf("Table to be solved:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(arr[i][j]->endpoint ==1){
if(arr[i][j]->path_number/10==0)
printf("| %d |",arr[i][j]->path_number);
else
printf("| %d|",arr[i][j]->path_number);
}
else if(arr[i][j]->path_number==0)
printf("| X |");
else
printf("| |");
}
printf("\n");
}
printf("\n\nThe solution to the above table:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(arr[i][j]->path_number != 0){
if(arr[i][j]->path_number/10==0)
printf("| %d |",arr[i][j]->path_number);
else
printf("| %d|",arr[i][j]->path_number);
}
else
printf("| X |");
}
printf("\n");
}
}
/******************/
/* Name: main()
Input: --void--
Output: returns 1
Description: The driver function.
*/
int main(void)
{
srand((unsigned int) time (NULL));
int i, j;
int ct = 1;
// Size of board
int n = 7;
node*** pointers= (node ***)malloc(n*sizeof(node **));
for (i=0; i<n; i++)
pointers[i] = (node **)malloc(n*sizeof(node *));
initboard(pointers, n);
while(1) {
i = addpath(pointers, n, ct);
if (i==0){
//printf("Failure\n");
break;
}
else {
ct++;
//printf("Success\n");
}
}
printtable(pointers,n);
return 0;
}
/***********************/