2 * laydomino.c: code for performing a domino (2x1 tile) layout of
3 * a given area of code.
13 * This function returns an array size w x h representing a grid:
14 * each grid[i] = j, where j is the other end of a 2x1 domino.
15 * If w*h is odd, one square will remain referring to itself.
18 int *domino_layout(int w
, int h
, random_state
*rs
)
20 int *grid
, *grid2
, *list
;
24 * Allocate space in which to lay the grid out.
26 grid
= snewn(wh
, int);
27 grid2
= snewn(wh
, int);
28 list
= snewn(2*wh
, int);
30 domino_layout_prealloc(w
, h
, rs
, grid
, grid2
, list
);
39 * As for domino_layout, but with preallocated buffers.
40 * grid and grid2 should be size w*h, and list size 2*w*h.
42 void domino_layout_prealloc(int w
, int h
, random_state
*rs
,
43 int *grid
, int *grid2
, int *list
)
45 int i
, j
, k
, m
, wh
= w
*h
, todo
, done
;
48 * To begin with, set grid[i] = i for all i to indicate
49 * that all squares are currently singletons. Later we'll
50 * set grid[i] to be the index of the other end of the
53 for (i
= 0; i
< wh
; i
++)
57 * Now prepare a list of the possible domino locations. There
58 * are w*(h-1) possible vertical locations, and (w-1)*h
59 * horizontal ones, for a total of 2*wh - h - w.
61 * I'm going to denote the vertical domino placement with
62 * its top in square i as 2*i, and the horizontal one with
63 * its left half in square i as 2*i+1.
66 for (j
= 0; j
< h
-1; j
++)
67 for (i
= 0; i
< w
; i
++)
68 list
[k
++] = 2 * (j
*w
+i
); /* vertical positions */
69 for (j
= 0; j
< h
; j
++)
70 for (i
= 0; i
< w
-1; i
++)
71 list
[k
++] = 2 * (j
*w
+i
) + 1; /* horizontal positions */
72 assert(k
== 2*wh
- h
- w
);
77 shuffle(list
, k
, sizeof(*list
), rs
);
80 * Work down the shuffled list, placing a domino everywhere
83 for (i
= 0; i
< k
; i
++) {
88 xy2
= xy
+ (horiz
? 1 : w
);
90 if (grid
[xy
] == xy
&& grid
[xy2
] == xy2
) {
92 * We can place this domino. Do so.
99 #ifdef GENERATION_DIAGNOSTICS
100 printf("generated initial layout\n");
104 * Now we've placed as many dominoes as we can immediately
105 * manage. There will be squares remaining, but they'll be
106 * singletons. So loop round and deal with the singletons
110 #ifdef GENERATION_DIAGNOSTICS
111 for (j
= 0; j
< h
; j
++) {
112 for (i
= 0; i
< w
; i
++) {
115 int c
= (v
== xy
+1 ? '[' : v
== xy
-1 ? ']' :
116 v
== xy
+w
? 'n' : v
== xy
-w
? 'U' : '.');
127 * First find a singleton square.
129 * Then breadth-first search out from the starting
130 * square. From that square (and any others we reach on
131 * the way), examine all four neighbours of the square.
132 * If one is an end of a domino, we move to the _other_
133 * end of that domino before looking at neighbours
134 * again. When we encounter another singleton on this
137 * This will give us a path of adjacent squares such
138 * that all but the two ends are covered in dominoes.
139 * So we can now shuffle every domino on the path up by
142 * (Chessboard colours are mathematically important
143 * here: we always end up pairing each singleton with a
144 * singleton of the other colour. However, we never
145 * have to track this manually, since it's
146 * automatically taken care of by the fact that we
147 * always make an even number of orthogonal moves.)
150 for (j
= 0; j
< wh
; j
++) {
153 i
= j
; /* start BFS here. */
157 break; /* if area is even, we have no more singletons;
158 if area is odd, we have one singleton.
159 either way, we're done. */
161 #ifdef GENERATION_DIAGNOSTICS
162 printf("starting b.f.s. at singleton %d\n", i
);
165 * Set grid2 to -1 everywhere. It will hold our
166 * distance-from-start values, and also our
167 * backtracking data, during the b.f.s.
169 for (j
= 0; j
< wh
; j
++)
171 grid2
[i
] = 0; /* starting square has distance zero */
174 * Start our to-do list of squares. It'll live in
175 * `list'; since the b.f.s can cover every square at
176 * most once there is no need for it to be circular.
177 * We'll just have two counters tracking the end of the
178 * list and the squares we've already dealt with.
185 * Now begin the b.f.s. loop.
187 while (done
< todo
) {
192 #ifdef GENERATION_DIAGNOSTICS
193 printf("b.f.s. iteration from %d\n", i
);
207 * To avoid directional bias, process the
208 * neighbours of this square in a random order.
210 shuffle(d
, nd
, sizeof(*d
), rs
);
212 for (j
= 0; j
< nd
; j
++) {
215 #ifdef GENERATION_DIAGNOSTICS
216 printf("found neighbouring singleton %d\n", k
);
219 break; /* found a target singleton! */
223 * We're moving through a domino here, so we
224 * have two entries in grid2 to fill with
225 * useful data. In grid[k] - the square
226 * adjacent to where we came from - I'm going
227 * to put the address _of_ the square we came
228 * from. In the other end of the domino - the
229 * square from which we will continue the
230 * search - I'm going to put the distance.
234 if (grid2
[m
] < 0 || grid2
[m
] > grid2
[i
]+1) {
235 #ifdef GENERATION_DIAGNOSTICS
236 printf("found neighbouring domino %d/%d\n", k
, m
);
238 grid2
[m
] = grid2
[i
]+1;
241 * And since we've now visited a new
242 * domino, add m to the to-do list.
251 #ifdef GENERATION_DIAGNOSTICS
252 printf("terminating b.f.s. loop, i = %d\n", i
);
257 i
= -1; /* just in case the loop terminates */
261 * We expect this b.f.s. to have found us a target
267 * Now we can follow the trail back to our starting
268 * singleton, re-laying dominoes as we go.
272 assert(j
>= 0 && j
< wh
);
277 #ifdef GENERATION_DIAGNOSTICS
278 printf("filling in domino %d/%d (next %d)\n", i
, j
, k
);
281 break; /* we've reached the other singleton */
284 #ifdef GENERATION_DIAGNOSTICS
285 printf("fixup path completed\n");
290 /* vim: set shiftwidth=4 :set textwidth=80: */