-
Notifications
You must be signed in to change notification settings - Fork 0
/
rect.c
3024 lines (2667 loc) · 93 KB
/
rect.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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* rect.c: Puzzle from nikoli.co.jp. You have a square grid with
* numbers in some squares; you must divide the square grid up into
* variously sized rectangles, such that every rectangle contains
* exactly one numbered square and the area of each rectangle is
* equal to the number contained in it.
*/
/*
* TODO:
*
* - Improve singleton removal.
* + It would be nice to limit the size of the generated
* rectangles in accordance with existing constraints such as
* the maximum rectangle size and the one about not
* generating a rectangle the full width or height of the
* grid.
* + This could be achieved by making a less random choice
* about which of the available options to use.
* + Alternatively, we could create our rectangle and then
* split it up.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include "puzzles.h"
enum {
COL_BACKGROUND,
COL_CORRECT,
COL_LINE,
COL_TEXT,
COL_GRID,
COL_DRAG, COL_DRAGERASE,
COL_CURSOR,
NCOLOURS
};
struct game_params {
int w, h;
float expandfactor;
bool unique;
};
#define INDEX(state, x, y) (((y) * (state)->w) + (x))
#define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
#define grid(state,x,y) index(state, (state)->grid, x, y)
#define vedge(state,x,y) index(state, (state)->vedge, x, y)
#define hedge(state,x,y) index(state, (state)->hedge, x, y)
#define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
(y) >= dy && (y) < (state)->h )
#define RANGE(state,x,y) CRANGE(state,x,y,0,0)
#define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
#define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
#define PREFERRED_TILE_SIZE 24
#define TILE_SIZE (ds->tilesize)
#ifdef SMALL_SCREEN
#define BORDER (2)
#else
#define BORDER (TILE_SIZE * 3 / 4)
#endif
#define CORNER_TOLERANCE 0.15F
#define CENTRE_TOLERANCE 0.15F
#define FLASH_TIME 0.13F
#define COORD(x) ( (x) * TILE_SIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
struct game_state {
int w, h;
int *grid; /* contains the numbers */
unsigned char *vedge; /* (w+1) x h */
unsigned char *hedge; /* w x (h+1) */
bool completed, cheated;
unsigned char *correct;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = ret->h = 7;
ret->expandfactor = 0.0F;
ret->unique = true;
return ret;
}
static bool game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
int w, h;
char buf[80];
switch (i) {
case 0: w = 7, h = 7; break;
case 1: w = 9, h = 9; break;
case 2: w = 11, h = 11; break;
case 3: w = 13, h = 13; break;
case 4: w = 15, h = 15; break;
#ifndef SMALL_SCREEN
case 5: w = 17, h = 17; break;
case 6: w = 19, h = 19; break;
#endif
default: return false;
}
sprintf(buf, "%dx%d", w, h);
*name = dupstr(buf);
*params = ret = snew(game_params);
ret->w = w;
ret->h = h;
ret->expandfactor = 0.0F;
ret->unique = true;
return true;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *ret, char const *string)
{
ret->w = ret->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
ret->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'e') {
string++;
ret->expandfactor = (float)atof(string);
while (*string &&
(*string == '.' || isdigit((unsigned char)*string))) string++;
}
if (*string == 'a') {
string++;
ret->unique = false;
}
}
static char *encode_params(const game_params *params, bool full)
{
char data[256];
sprintf(data, "%dx%d", params->w, params->h);
if (full && params->expandfactor)
sprintf(data + strlen(data), "e%g", params->expandfactor);
if (full && !params->unique)
strcat(data, "a");
return dupstr(data);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(5, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].u.string.sval = dupstr(buf);
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Expansion factor";
ret[2].type = C_STRING;
sprintf(buf, "%g", params->expandfactor);
ret[2].u.string.sval = dupstr(buf);
ret[3].name = "Ensure unique solution";
ret[3].type = C_BOOLEAN;
ret[3].u.boolean.bval = params->unique;
ret[4].name = NULL;
ret[4].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].u.string.sval);
ret->h = atoi(cfg[1].u.string.sval);
ret->expandfactor = (float)atof(cfg[2].u.string.sval);
ret->unique = cfg[3].u.boolean.bval;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if (params->w <= 0 || params->h <= 0)
return "Width and height must both be greater than zero";
if (params->w > INT_MAX / params->h)
return "Width times height must not be unreasonably large";
if (params->w*params->h < 2)
return "Grid area must be greater than one";
if (params->expandfactor < 0.0F)
return "Expansion factor may not be negative";
return NULL;
}
struct point {
int x, y;
};
struct rect {
int x, y;
int w, h;
};
struct rectlist {
struct rect *rects;
int n;
};
struct numberdata {
int area;
int npoints;
struct point *points;
};
/* ----------------------------------------------------------------------
* Solver for Rectangles games.
*
* This solver is souped up beyond the needs of actually _solving_
* a puzzle. It is also designed to cope with uncertainty about
* where the numbers have been placed. This is because I run it on
* my generated grids _before_ placing the numbers, and have it
* tell me where I need to place the numbers to ensure a unique
* solution.
*/
static void remove_rect_placement(int w, int h,
struct rectlist *rectpositions,
int *overlaps,
int rectnum, int placement)
{
int x, y, xx, yy;
#ifdef SOLVER_DIAGNOSTICS
printf("ruling out rect %d placement at %d,%d w=%d h=%d\n", rectnum,
rectpositions[rectnum].rects[placement].x,
rectpositions[rectnum].rects[placement].y,
rectpositions[rectnum].rects[placement].w,
rectpositions[rectnum].rects[placement].h);
#endif
/*
* Decrement each entry in the overlaps array to reflect the
* removal of this rectangle placement.
*/
for (yy = 0; yy < rectpositions[rectnum].rects[placement].h; yy++) {
y = yy + rectpositions[rectnum].rects[placement].y;
for (xx = 0; xx < rectpositions[rectnum].rects[placement].w; xx++) {
x = xx + rectpositions[rectnum].rects[placement].x;
assert(overlaps[(rectnum * h + y) * w + x] != 0);
if (overlaps[(rectnum * h + y) * w + x] > 0)
overlaps[(rectnum * h + y) * w + x]--;
}
}
/*
* Remove the placement from the list of positions for that
* rectangle, by interchanging it with the one on the end.
*/
if (placement < rectpositions[rectnum].n - 1) {
struct rect t;
t = rectpositions[rectnum].rects[rectpositions[rectnum].n - 1];
rectpositions[rectnum].rects[rectpositions[rectnum].n - 1] =
rectpositions[rectnum].rects[placement];
rectpositions[rectnum].rects[placement] = t;
}
rectpositions[rectnum].n--;
}
static void remove_number_placement(int w, int h, struct numberdata *number,
int index, int *rectbyplace)
{
/*
* Remove the entry from the rectbyplace array.
*/
rectbyplace[number->points[index].y * w + number->points[index].x] = -1;
/*
* Remove the placement from the list of candidates for that
* number, by interchanging it with the one on the end.
*/
if (index < number->npoints - 1) {
struct point t;
t = number->points[number->npoints - 1];
number->points[number->npoints - 1] = number->points[index];
number->points[index] = t;
}
number->npoints--;
}
/*
* Returns 0 for failure to solve due to inconsistency; 1 for
* success; 2 for failure to complete a solution due to either
* ambiguity or it being too difficult.
*/
static int rect_solver(int w, int h, int nrects, struct numberdata *numbers,
unsigned char *hedge, unsigned char *vedge,
random_state *rs)
{
struct rectlist *rectpositions;
int *overlaps, *rectbyplace, *workspace;
int i, ret;
/*
* Start by setting up a list of candidate positions for each
* rectangle.
*/
rectpositions = snewn(nrects, struct rectlist);
for (i = 0; i < nrects; i++) {
int rw, rh, area = numbers[i].area;
int j, minx, miny, maxx, maxy;
struct rect *rlist;
int rlistn, rlistsize;
/*
* For each rectangle, begin by finding the bounding
* rectangle of its candidate number placements.
*/
maxx = maxy = -1;
minx = w;
miny = h;
for (j = 0; j < numbers[i].npoints; j++) {
if (minx > numbers[i].points[j].x) minx = numbers[i].points[j].x;
if (miny > numbers[i].points[j].y) miny = numbers[i].points[j].y;
if (maxx < numbers[i].points[j].x) maxx = numbers[i].points[j].x;
if (maxy < numbers[i].points[j].y) maxy = numbers[i].points[j].y;
}
/*
* Now loop over all possible rectangle placements
* overlapping a point within that bounding rectangle;
* ensure each one actually contains a candidate number
* placement, and add it to the list.
*/
rlist = NULL;
rlistn = rlistsize = 0;
for (rw = 1; rw <= area && rw <= w; rw++) {
int x, y;
if (area % rw)
continue;
rh = area / rw;
if (rh > h)
continue;
for (y = miny - rh + 1; y <= maxy; y++) {
if (y < 0 || y+rh > h)
continue;
for (x = minx - rw + 1; x <= maxx; x++) {
if (x < 0 || x+rw > w)
continue;
/*
* See if we can find a candidate number
* placement within this rectangle.
*/
for (j = 0; j < numbers[i].npoints; j++)
if (numbers[i].points[j].x >= x &&
numbers[i].points[j].x < x+rw &&
numbers[i].points[j].y >= y &&
numbers[i].points[j].y < y+rh)
break;
if (j < numbers[i].npoints) {
/*
* Add this to the list of candidate
* placements for this rectangle.
*/
if (rlistn >= rlistsize) {
rlistsize = rlistn + 32;
rlist = sresize(rlist, rlistsize, struct rect);
}
rlist[rlistn].x = x;
rlist[rlistn].y = y;
rlist[rlistn].w = rw;
rlist[rlistn].h = rh;
#ifdef SOLVER_DIAGNOSTICS
printf("rect %d [area %d]: candidate position at"
" %d,%d w=%d h=%d\n",
i, area, x, y, rw, rh);
#endif
rlistn++;
}
}
}
}
rectpositions[i].rects = rlist;
rectpositions[i].n = rlistn;
}
/*
* Next, construct a multidimensional array tracking how many
* candidate positions for each rectangle overlap each square.
*
* Indexing of this array is by the formula
*
* overlaps[(rectindex * h + y) * w + x]
*
* A positive or zero value indicates what it sounds as if it
* should; -1 indicates that this square _cannot_ be part of
* this rectangle; and -2 indicates that it _definitely_ is
* (which is distinct from 1, because one might very well know
* that _if_ square S is part of rectangle R then it must be
* because R is placed in a certain position without knowing
* that it definitely _is_).
*/
overlaps = snewn(nrects * w * h, int);
memset(overlaps, 0, nrects * w * h * sizeof(int));
for (i = 0; i < nrects; i++) {
int j;
for (j = 0; j < rectpositions[i].n; j++) {
int xx, yy;
for (yy = 0; yy < rectpositions[i].rects[j].h; yy++)
for (xx = 0; xx < rectpositions[i].rects[j].w; xx++)
overlaps[(i * h + yy+rectpositions[i].rects[j].y) * w +
xx+rectpositions[i].rects[j].x]++;
}
}
/*
* Also we want an array covering the grid once, to make it
* easy to figure out which squares are candidate number
* placements for which rectangles. (The existence of this
* single array assumes that no square starts off as a
* candidate number placement for more than one rectangle. This
* assumption is justified, because this solver is _either_
* used to solve real problems - in which case there is a
* single placement for every number - _or_ used to decide on
* number placements for a new puzzle, in which case each
* number's placements are confined to the intended position of
* the rectangle containing that number.)
*/
rectbyplace = snewn(w * h, int);
for (i = 0; i < w*h; i++)
rectbyplace[i] = -1;
for (i = 0; i < nrects; i++) {
int j;
for (j = 0; j < numbers[i].npoints; j++) {
int x = numbers[i].points[j].x;
int y = numbers[i].points[j].y;
assert(rectbyplace[y * w + x] == -1);
rectbyplace[y * w + x] = i;
}
}
workspace = snewn(nrects, int);
/*
* Now run the actual deduction loop.
*/
while (1) {
bool done_something = false;
#ifdef SOLVER_DIAGNOSTICS
printf("starting deduction loop\n");
for (i = 0; i < nrects; i++) {
printf("rect %d overlaps:\n", i);
{
int x, y;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
printf("%3d", overlaps[(i * h + y) * w + x]);
}
printf("\n");
}
}
}
printf("rectbyplace:\n");
{
int x, y;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
printf("%3d", rectbyplace[y * w + x]);
}
printf("\n");
}
}
#endif
/*
* Housekeeping. Look for rectangles whose number has only
* one candidate position left, and mark that square as
* known if it isn't already.
*/
for (i = 0; i < nrects; i++) {
if (numbers[i].npoints == 1) {
int x = numbers[i].points[0].x;
int y = numbers[i].points[0].y;
if (overlaps[(i * h + y) * w + x] >= -1) {
int j;
if (overlaps[(i * h + y) * w + x] <= 0) {
ret = 0; /* inconsistency */
goto cleanup;
}
#ifdef SOLVER_DIAGNOSTICS
printf("marking %d,%d as known for rect %d"
" (sole remaining number position)\n", x, y, i);
#endif
for (j = 0; j < nrects; j++)
overlaps[(j * h + y) * w + x] = -1;
overlaps[(i * h + y) * w + x] = -2;
}
}
}
/*
* Now look at the intersection of all possible placements
* for each rectangle, and mark all squares in that
* intersection as known for that rectangle if they aren't
* already.
*/
for (i = 0; i < nrects; i++) {
int minx, miny, maxx, maxy, xx, yy, j;
minx = miny = 0;
maxx = w;
maxy = h;
for (j = 0; j < rectpositions[i].n; j++) {
int x = rectpositions[i].rects[j].x;
int y = rectpositions[i].rects[j].y;
int w = rectpositions[i].rects[j].w;
int h = rectpositions[i].rects[j].h;
if (minx < x) minx = x;
if (miny < y) miny = y;
if (maxx > x+w) maxx = x+w;
if (maxy > y+h) maxy = y+h;
}
for (yy = miny; yy < maxy; yy++)
for (xx = minx; xx < maxx; xx++)
if (overlaps[(i * h + yy) * w + xx] >= -1) {
if (overlaps[(i * h + yy) * w + xx] <= 0) {
ret = 0; /* inconsistency */
goto cleanup;
}
#ifdef SOLVER_DIAGNOSTICS
printf("marking %d,%d as known for rect %d"
" (intersection of all placements)\n",
xx, yy, i);
#endif
for (j = 0; j < nrects; j++)
overlaps[(j * h + yy) * w + xx] = -1;
overlaps[(i * h + yy) * w + xx] = -2;
}
}
/*
* Rectangle-focused deduction. Look at each rectangle in
* turn and try to rule out some of its candidate
* placements.
*/
for (i = 0; i < nrects; i++) {
int j;
for (j = 0; j < rectpositions[i].n; j++) {
int xx, yy, k;
bool del = false;
for (k = 0; k < nrects; k++)
workspace[k] = 0;
for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
int y = yy + rectpositions[i].rects[j].y;
for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
int x = xx + rectpositions[i].rects[j].x;
if (overlaps[(i * h + y) * w + x] == -1) {
/*
* This placement overlaps a square
* which is _known_ to be part of
* another rectangle. Therefore we must
* rule it out.
*/
#ifdef SOLVER_DIAGNOSTICS
printf("rect %d placement at %d,%d w=%d h=%d "
"contains %d,%d which is known-other\n", i,
rectpositions[i].rects[j].x,
rectpositions[i].rects[j].y,
rectpositions[i].rects[j].w,
rectpositions[i].rects[j].h,
x, y);
#endif
del = true;
}
if (rectbyplace[y * w + x] != -1) {
/*
* This placement overlaps one of the
* candidate number placements for some
* rectangle. Count it.
*/
workspace[rectbyplace[y * w + x]]++;
}
}
}
if (!del) {
/*
* If we haven't ruled this placement out
* already, see if it overlaps _all_ of the
* candidate number placements for any
* rectangle. If so, we can rule it out.
*/
for (k = 0; k < nrects; k++)
if (k != i && workspace[k] == numbers[k].npoints) {
#ifdef SOLVER_DIAGNOSTICS
printf("rect %d placement at %d,%d w=%d h=%d "
"contains all number points for rect %d\n",
i,
rectpositions[i].rects[j].x,
rectpositions[i].rects[j].y,
rectpositions[i].rects[j].w,
rectpositions[i].rects[j].h,
k);
#endif
del = true;
break;
}
/*
* Failing that, see if it overlaps at least
* one of the candidate number placements for
* itself! (This might not be the case if one
* of those number placements has been removed
* recently.).
*/
if (!del && workspace[i] == 0) {
#ifdef SOLVER_DIAGNOSTICS
printf("rect %d placement at %d,%d w=%d h=%d "
"contains none of its own number points\n",
i,
rectpositions[i].rects[j].x,
rectpositions[i].rects[j].y,
rectpositions[i].rects[j].w,
rectpositions[i].rects[j].h);
#endif
del = true;
}
}
if (del) {
remove_rect_placement(w, h, rectpositions, overlaps, i, j);
j--; /* don't skip over next placement */
done_something = true;
}
}
}
/*
* Square-focused deduction. Look at each square not marked
* as known, and see if there are any which can only be
* part of a single rectangle.
*/
{
int x, y, n, index;
for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
/* Known squares are marked as <0 everywhere, so we only need
* to check the overlaps entry for rect 0. */
if (overlaps[y * w + x] < 0)
continue; /* known already */
n = 0;
index = -1;
for (i = 0; i < nrects; i++)
if (overlaps[(i * h + y) * w + x] > 0)
n++, index = i;
if (n == 1) {
int j;
/*
* Now we can rule out all placements for
* rectangle `index' which _don't_ contain
* square x,y.
*/
#ifdef SOLVER_DIAGNOSTICS
printf("square %d,%d can only be in rectangle %d\n",
x, y, index);
#endif
for (j = 0; j < rectpositions[index].n; j++) {
struct rect *r = &rectpositions[index].rects[j];
if (x >= r->x && x < r->x + r->w &&
y >= r->y && y < r->y + r->h)
continue; /* this one is OK */
remove_rect_placement(w, h, rectpositions, overlaps,
index, j);
j--; /* don't skip over next placement */
done_something = true;
}
}
}
}
/*
* If we've managed to deduce anything by normal means,
* loop round again and see if there's more to be done.
* Only if normal deduction has completely failed us should
* we now move on to narrowing down the possible number
* placements.
*/
if (done_something)
continue;
/*
* Now we have done everything we can with the current set
* of number placements. So we need to winnow the number
* placements so as to narrow down the possibilities. We do
* this by searching for a candidate placement (of _any_
* rectangle) which overlaps a candidate placement of the
* number for some other rectangle.
*/
if (rs) {
struct rpn {
int rect;
int placement;
int number;
} *rpns = NULL;
size_t nrpns = 0, rpnsize = 0;
int j;
for (i = 0; i < nrects; i++) {
for (j = 0; j < rectpositions[i].n; j++) {
int xx, yy;
for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
int y = yy + rectpositions[i].rects[j].y;
for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
int x = xx + rectpositions[i].rects[j].x;
if (rectbyplace[y * w + x] >= 0 &&
rectbyplace[y * w + x] != i) {
/*
* Add this to the list of
* winnowing possibilities.
*/
if (nrpns >= rpnsize) {
rpnsize = rpnsize * 3 / 2 + 32;
rpns = sresize(rpns, rpnsize, struct rpn);
}
rpns[nrpns].rect = i;
rpns[nrpns].placement = j;
rpns[nrpns].number = rectbyplace[y * w + x];
nrpns++;
}
}
}
}
}
#ifdef SOLVER_DIAGNOSTICS
printf("%d candidate rect placements we could eliminate\n", nrpns);
#endif
if (nrpns > 0) {
/*
* Now choose one of these unwanted rectangle
* placements, and eliminate it.
*/
int index = random_upto(rs, nrpns);
int k, m;
struct rpn rpn = rpns[index];
struct rect r;
sfree(rpns);
i = rpn.rect;
j = rpn.placement;
k = rpn.number;
r = rectpositions[i].rects[j];
/*
* We rule out placement j of rectangle i by means
* of removing all of rectangle k's candidate
* number placements which do _not_ overlap it.
* This will ensure that it is eliminated during
* the next pass of rectangle-focused deduction.
*/
#ifdef SOLVER_DIAGNOSTICS
printf("ensuring number for rect %d is within"
" rect %d's placement at %d,%d w=%d h=%d\n",
k, i, r.x, r.y, r.w, r.h);
#endif
for (m = 0; m < numbers[k].npoints; m++) {
int x = numbers[k].points[m].x;
int y = numbers[k].points[m].y;
if (x < r.x || x >= r.x + r.w ||
y < r.y || y >= r.y + r.h) {
#ifdef SOLVER_DIAGNOSTICS
printf("eliminating number for rect %d at %d,%d\n",
k, x, y);
#endif
remove_number_placement(w, h, &numbers[k],
m, rectbyplace);
m--; /* don't skip the next one */
done_something = true;
}
}
}
}
if (!done_something) {
#ifdef SOLVER_DIAGNOSTICS
printf("terminating deduction loop\n");
#endif
break;
}
}
cleanup:
ret = 1;
for (i = 0; i < nrects; i++) {
#ifdef SOLVER_DIAGNOSTICS
printf("rect %d has %d possible placements\n",
i, rectpositions[i].n);
#endif
if (rectpositions[i].n <= 0) {
ret = 0; /* inconsistency */
} else if (rectpositions[i].n > 1) {
ret = 2; /* remaining uncertainty */
} else if (hedge && vedge) {
/*
* Place the rectangle in its only possible position.
*/
int x, y;
struct rect *r = &rectpositions[i].rects[0];
for (y = 0; y < r->h; y++) {
if (r->x > 0)
vedge[(r->y+y) * w + r->x] = 1;
if (r->x+r->w < w)
vedge[(r->y+y) * w + r->x+r->w] = 1;
}
for (x = 0; x < r->w; x++) {
if (r->y > 0)
hedge[r->y * w + r->x+x] = 1;
if (r->y+r->h < h)
hedge[(r->y+r->h) * w + r->x+x] = 1;
}
}
}
/*
* Free up all allocated storage.
*/
sfree(workspace);
sfree(rectbyplace);
sfree(overlaps);
for (i = 0; i < nrects; i++)
sfree(rectpositions[i].rects);
sfree(rectpositions);
return ret;
}
/* ----------------------------------------------------------------------
* Grid generation code.
*/
/*
* This function does one of two things. If passed r==NULL, it
* counts the number of possible rectangles which cover the given
* square, and returns it in *n. If passed r!=NULL then it _reads_
* *n to find an index, counts the possible rectangles until it
* reaches the nth, and writes it into r.
*
* `scratch' is expected to point to an array of 2 * params->w
* ints, used internally as scratch space (and passed in like this
* to avoid re-allocating and re-freeing it every time round a
* tight loop).
*/
static void enum_rects(game_params *params, int *grid, struct rect *r, int *n,
int sx, int sy, int *scratch)
{
int rw, rh, mw, mh;
int x, y, dx, dy;
int maxarea, realmaxarea;
int index = 0;
int *top, *bottom;
/*
* Maximum rectangle area is 1/6 of total grid size, unless
* this means we can't place any rectangles at all in which
* case we set it to 2 at minimum.
*/
maxarea = params->w * params->h / 6;
if (maxarea < 2)
maxarea = 2;
/*
* Scan the grid to find the limits of the region within which
* any rectangle containing this point must fall. This will
* save us trawling the inside of every rectangle later on to
* see if it contains any used squares.
*/
top = scratch;
bottom = scratch + params->w;
for (dy = -1; dy <= +1; dy += 2) {
int *array = (dy == -1 ? top : bottom);
for (dx = -1; dx <= +1; dx += 2) {
for (x = sx; x >= 0 && x < params->w; x += dx) {
array[x] = -2 * params->h * dy;
for (y = sy; y >= 0 && y < params->h; y += dy) {
if (index(params, grid, x, y) == -1 &&
(x == sx || dy*y <= dy*array[x-dx]))
array[x] = y;
else
break;
}
}
}
}
/*
* Now scan again to work out the largest rectangles we can fit
* in the grid, so that we can terminate the following loops
* early once we get down to not having much space left in the
* grid.
*/
realmaxarea = 0;
for (x = 0; x < params->w; x++) {
int x2;
rh = bottom[x] - top[x] + 1;
if (rh <= 0)
continue; /* no rectangles can start here */
dx = (x > sx ? -1 : +1);
for (x2 = x; x2 >= 0 && x2 < params->w; x2 += dx)
if (bottom[x2] < bottom[x] || top[x2] > top[x])
break;
rw = abs(x2 - x);
if (realmaxarea < rw * rh)
realmaxarea = rw * rh;
}
if (realmaxarea > maxarea)
realmaxarea = maxarea;