divvy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * Library code to divide up a rectangle into a number of equally
  3. * sized ominoes, in a random fashion.
  4. *
  5. * Could use this for generating solved grids of
  6. * http://www.nikoli.co.jp/ja/puzzles/block_puzzle/
  7. * or for generating the playfield for Jigsaw Sudoku.
  8. */
  9. /*
  10. * This code is restricted to simply connected solutions: that is,
  11. * no single polyomino may completely surround another (not even
  12. * with a corner visible to the outside world, in the sense that a
  13. * 7-omino can `surround' a single square).
  14. *
  15. * It's tempting to think that this is a natural consequence of
  16. * all the ominoes being the same size - after all, a division of
  17. * anything into 7-ominoes must necessarily have all of them
  18. * simply connected, because if one was not then the 1-square
  19. * space in the middle could not be part of any 7-omino - but in
  20. * fact, for sufficiently large k, it is perfectly possible for a
  21. * k-omino to completely surround another k-omino. A simple
  22. * example is this one with two 25-ominoes:
  23. *
  24. * +--+--+--+--+--+--+--+
  25. * | |
  26. * + +--+--+--+--+--+ +
  27. * | | | |
  28. * + + + +
  29. * | | | |
  30. * + + + +--+
  31. * | | | |
  32. * + + + +--+
  33. * | | | |
  34. * + + + +
  35. * | | | |
  36. * + +--+--+--+--+--+ +
  37. * | |
  38. * +--+--+--+--+--+--+--+
  39. *
  40. * I claim the smallest k which can manage this is 23. More
  41. * formally:
  42. *
  43. * If a k-omino P is completely surrounded by another k-omino Q,
  44. * such that every edge of P borders on Q, then k >= 23.
  45. *
  46. * Proof:
  47. *
  48. * It's relatively simple to find the largest _rectangle_ a
  49. * k-omino can enclose. So I'll construct my proof in two parts:
  50. * firstly, show that no 22-omino or smaller can enclose a
  51. * rectangle as large as itself, and secondly, show that no
  52. * polyomino can enclose a larger non-rectangle than a rectangle.
  53. *
  54. * The first of those claims:
  55. *
  56. * To surround an m x n rectangle, a polyomino must have 2m
  57. * squares along the two m-sides of the rectangle, 2n squares
  58. * along the two n-sides, and must fill in at least three of the
  59. * corners in order to be connected. Thus, 2(m+n)+3 <= k. We wish
  60. * to find the largest value of mn subject to that constraint, and
  61. * it's clear that this is achieved when m and n are as close to
  62. * equal as possible. (If they aren't, WLOG suppose m < n; then
  63. * (m+1)(n-1) = mn + n - m - 1 >= mn, with equality only when
  64. * m=n-1.)
  65. *
  66. * So the area of the largest rectangle which can be enclosed by a
  67. * k-omino is given by floor(k'/2) * ceil(k'/2), where k' =
  68. * (k-3)/2. This is a monotonic function in k, so there will be a
  69. * unique point at which it goes from being smaller than k to
  70. * being larger than k. That point is between 22 (maximum area 20)
  71. * and 23 (maximum area 25).
  72. *
  73. * The second claim:
  74. *
  75. * Suppose we have an inner polyomino P surrounded by an outer
  76. * polyomino Q. I seek to show that if P is non-rectangular, then
  77. * P is also non-maximal, in the sense that we can transform P and
  78. * Q into a new pair of polyominoes in which P is larger and Q is
  79. * at most the same size.
  80. *
  81. * Consider walking along the boundary of P in a clockwise
  82. * direction. (We may assume, of course, that there is only _one_
  83. * boundary of P, i.e. P has no hole in the middle. If it does
  84. * have a hole in the middle, it's _trivially_ non-maximal because
  85. * we can just fill the hole in!) Our walk will take us along many
  86. * edges between squares; sometimes we might turn left, and
  87. * certainly sometimes we will turn right. Always there will be a
  88. * square of P on our right, and a square of Q on our left.
  89. *
  90. * The net angle through which we turn during the entire walk must
  91. * add up to 360 degrees rightwards. So if there are no left
  92. * turns, then we must turn right exactly four times, meaning we
  93. * have described a rectangle. Hence, if P is _not_ rectangular,
  94. * then there must have been a left turn at some point. A left
  95. * turn must mean we walk along two edges of the same square of Q.
  96. *
  97. * Thus, there is some square X in Q which is adjacent to two
  98. * diagonally separated squares in P. Let us call those two
  99. * squares N and E; let us refer to the other two neighbours of X
  100. * as S and W; let us refer to the other mutual neighbour of S and
  101. * W as D; and let us refer to the other mutual neighbour of S and
  102. * E as Y. In other words, we have named seven squares, arranged
  103. * thus:
  104. *
  105. * N
  106. * W X E
  107. * D S Y
  108. *
  109. * where N and E are in P, and X is in Q.
  110. *
  111. * Clearly at least one of W and S must be in Q (because otherwise
  112. * X would not be connected to any other square in Q, and would
  113. * hence have to be the whole of Q; and evidently if Q were a
  114. * 1-omino it could not enclose _anything_). So we divide into
  115. * cases:
  116. *
  117. * If both W and S are in Q, then we take X out of Q and put it in
  118. * P, which does not expose any edge of P. If this disconnects Q,
  119. * then we can reconnect it by adding D to Q.
  120. *
  121. * If only one of W and S is in Q, then wlog let it be W. If S is
  122. * in _P_, then we have a particularly easy case: we can simply
  123. * take X out of Q and add it to P, and this cannot disconnect X
  124. * since X was a leaf square of Q.
  125. *
  126. * Our remaining case is that W is in Q and S is in neither P nor
  127. * Q. Again we take X out of Q and put it in P; we also add S to
  128. * Q. This ensures we do not expose an edge of P, but we must now
  129. * prove that S is adjacent to some other existing square of Q so
  130. * that we haven't disconnected Q by adding it.
  131. *
  132. * To do this, we recall that we walked along the edge XE, and
  133. * then turned left to walk along XN. So just before doing all
  134. * that, we must have reached the corner XSE, and we must have
  135. * done it by walking along one of the three edges meeting at that
  136. * corner which are _not_ XE. It can't have been SY, since S would
  137. * then have been on our left and it isn't in Q; and it can't have
  138. * been XS, since S would then have been on our right and it isn't
  139. * in P. So it must have been YE, in which case Y was on our left,
  140. * and hence is in Q.
  141. *
  142. * So in all cases we have shown that we can take X out of Q and
  143. * add it to P, and add at most one square to Q to restore the
  144. * containment and connectedness properties. Hence, we can keep
  145. * doing this until we run out of left turns and P becomes
  146. * rectangular. []
  147. *
  148. * ------------
  149. *
  150. * Anyway, that entire proof was a bit of a sidetrack. The point
  151. * is, although constructions of this type are possible for
  152. * sufficiently large k, divvy_rectangle() will never generate
  153. * them. This could be considered a weakness for some purposes, in
  154. * the sense that we can't generate all possible divisions.
  155. * However, there are many divisions which we are highly unlikely
  156. * to generate anyway, so in practice it probably isn't _too_ bad.
  157. *
  158. * If I wanted to fix this issue, I would have to make the rules
  159. * more complicated for determining when a square can safely be
  160. * _removed_ from a polyomino. Adding one becomes easier (a square
  161. * may be added to a polyomino iff it is 4-adjacent to any square
  162. * currently part of the polyomino, and the current test for loop
  163. * formation may be dispensed with), but to determine which
  164. * squares may be removed we must now resort to analysis of the
  165. * overall structure of the polyomino rather than the simple local
  166. * properties we can currently get away with measuring.
  167. */
  168. /*
  169. * Possible improvements which might cut the fail rate:
  170. *
  171. * - instead of picking one omino to extend in an iteration, try
  172. * them all in succession (in a randomised order)
  173. *
  174. * - (for real rigour) instead of bfsing over ominoes, bfs over
  175. * the space of possible _removed squares_. That way we aren't
  176. * limited to randomly choosing a single square to remove from
  177. * an omino and failing if that particular square doesn't
  178. * happen to work.
  179. *
  180. * However, I don't currently think it's necessary to do either of
  181. * these, because the failure rate is already low enough to be
  182. * easily tolerable, under all circumstances I've been able to
  183. * think of.
  184. */
  185. #include <assert.h>
  186. #include <stdio.h>
  187. #include <stdlib.h>
  188. #include <stddef.h>
  189. #include "puzzles.h"
  190. /*
  191. * Subroutine which implements a function used in computing both
  192. * whether a square can safely be added to an omino, and whether
  193. * it can safely be removed.
  194. *
  195. * We enumerate the eight squares 8-adjacent to this one, in
  196. * cyclic order. We go round that loop and count the number of
  197. * times we find a square owned by the target omino next to one
  198. * not owned by it. We then return success iff that count is 2.
  199. *
  200. * When adding a square to an omino, this is precisely the
  201. * criterion which tells us that adding the square won't leave a
  202. * hole in the middle of the omino. (If it did, then things get
  203. * more complicated; see above.)
  204. *
  205. * When removing a square from an omino, the _same_ criterion
  206. * tells us that removing the square won't disconnect the omino.
  207. * (This only works _because_ we've ensured the omino is simply
  208. * connected.)
  209. */
  210. static bool addremcommon(int w, int h, int x, int y, int *own, int val)
  211. {
  212. int neighbours[8];
  213. int dir, count;
  214. for (dir = 0; dir < 8; dir++) {
  215. int dx = ((dir & 3) == 2 ? 0 : dir > 2 && dir < 6 ? +1 : -1);
  216. int dy = ((dir & 3) == 0 ? 0 : dir < 4 ? -1 : +1);
  217. int sx = x+dx, sy = y+dy;
  218. if (sx < 0 || sx >= w || sy < 0 || sy >= h)
  219. neighbours[dir] = -1; /* outside the grid */
  220. else
  221. neighbours[dir] = own[sy*w+sx];
  222. }
  223. /*
  224. * To begin with, check 4-adjacency.
  225. */
  226. if (neighbours[0] != val && neighbours[2] != val &&
  227. neighbours[4] != val && neighbours[6] != val)
  228. return false;
  229. count = 0;
  230. for (dir = 0; dir < 8; dir++) {
  231. int next = (dir + 1) & 7;
  232. bool gotthis = (neighbours[dir] == val);
  233. bool gotnext = (neighbours[next] == val);
  234. if (gotthis != gotnext)
  235. count++;
  236. }
  237. return (count == 2);
  238. }
  239. /*
  240. * w and h are the dimensions of the rectangle.
  241. *
  242. * k is the size of the required ominoes. (So k must divide w*h,
  243. * of course.)
  244. *
  245. * The returned result is a w*h-sized dsf.
  246. *
  247. * In both of the above suggested use cases, the user would
  248. * probably want w==h==k, but that isn't a requirement.
  249. */
  250. DSF *divvy_rectangle_attempt(int w, int h, int k, random_state *rs)
  251. {
  252. int *order, *queue, *tmp, *own, *sizes, *addable;
  253. DSF *retdsf, *tmpdsf;
  254. bool *removable;
  255. int wh = w*h;
  256. int i, j, n, x, y, qhead, qtail;
  257. n = wh / k;
  258. assert(wh == k*n);
  259. order = snewn(wh, int);
  260. tmp = snewn(wh, int);
  261. own = snewn(wh, int);
  262. sizes = snewn(n, int);
  263. queue = snewn(n, int);
  264. addable = snewn(wh*4, int);
  265. removable = snewn(wh, bool);
  266. retdsf = tmpdsf = NULL;
  267. /*
  268. * Permute the grid squares into a random order, which will be
  269. * used for iterating over the grid whenever we need to search
  270. * for something. This prevents directional bias and arranges
  271. * for the answer to be non-deterministic.
  272. */
  273. for (i = 0; i < wh; i++)
  274. order[i] = i;
  275. shuffle(order, wh, sizeof(*order), rs);
  276. /*
  277. * Begin by choosing a starting square at random for each
  278. * omino.
  279. */
  280. for (i = 0; i < wh; i++) {
  281. own[i] = -1;
  282. }
  283. for (i = 0; i < n; i++) {
  284. own[order[i]] = i;
  285. sizes[i] = 1;
  286. }
  287. /*
  288. * Now repeatedly pick a random omino which isn't already at
  289. * the target size, and find a way to expand it by one. This
  290. * may involve stealing a square from another omino, in which
  291. * case we then re-expand that omino, forming a chain of
  292. * square-stealing which terminates in an as yet unclaimed
  293. * square. Hence every successful iteration around this loop
  294. * causes the number of unclaimed squares to drop by one, and
  295. * so the process is bounded in duration.
  296. */
  297. while (1) {
  298. #ifdef DIVVY_DIAGNOSTICS
  299. {
  300. int x, y;
  301. printf("Top of loop. Current grid:\n");
  302. for (y = 0; y < h; y++) {
  303. for (x = 0; x < w; x++)
  304. printf("%3d", own[y*w+x]);
  305. printf("\n");
  306. }
  307. }
  308. #endif
  309. /*
  310. * Go over the grid and figure out which squares can
  311. * safely be added to, or removed from, each omino. We
  312. * don't take account of other ominoes in this process, so
  313. * we will often end up knowing that a square can be
  314. * poached from one omino by another.
  315. *
  316. * For each square, there may be up to four ominoes to
  317. * which it can be added (those to which it is
  318. * 4-adjacent).
  319. */
  320. for (y = 0; y < h; y++) {
  321. for (x = 0; x < w; x++) {
  322. int yx = y*w+x;
  323. int curr = own[yx];
  324. int dir;
  325. if (curr < 0) {
  326. removable[yx] = false; /* can't remove if not owned! */
  327. } else if (sizes[curr] == 1) {
  328. removable[yx] = true; /* can always remove a singleton */
  329. } else {
  330. /*
  331. * See if this square can be removed from its
  332. * omino without disconnecting it.
  333. */
  334. removable[yx] = addremcommon(w, h, x, y, own, curr);
  335. }
  336. for (dir = 0; dir < 4; dir++) {
  337. int dx = (dir == 0 ? -1 : dir == 1 ? +1 : 0);
  338. int dy = (dir == 2 ? -1 : dir == 3 ? +1 : 0);
  339. int sx = x + dx, sy = y + dy;
  340. int syx = sy*w+sx;
  341. addable[yx*4+dir] = -1;
  342. if (sx < 0 || sx >= w || sy < 0 || sy >= h)
  343. continue; /* no omino here! */
  344. if (own[syx] < 0)
  345. continue; /* also no omino here */
  346. if (own[syx] == own[yx])
  347. continue; /* we already got one */
  348. if (!addremcommon(w, h, x, y, own, own[syx]))
  349. continue; /* would non-simply connect the omino */
  350. addable[yx*4+dir] = own[syx];
  351. }
  352. }
  353. }
  354. for (i = j = 0; i < n; i++)
  355. if (sizes[i] < k)
  356. tmp[j++] = i;
  357. if (j == 0)
  358. break; /* all ominoes are complete! */
  359. j = tmp[random_upto(rs, j)];
  360. #ifdef DIVVY_DIAGNOSTICS
  361. printf("Trying to extend %d\n", j);
  362. #endif
  363. /*
  364. * So we're trying to expand omino j. We breadth-first
  365. * search out from j across the space of ominoes.
  366. *
  367. * For bfs purposes, we use two elements of tmp per omino:
  368. * tmp[2*i+0] tells us which omino we got to i from, and
  369. * tmp[2*i+1] numbers the grid square that omino stole
  370. * from us.
  371. *
  372. * This requires that wh (the size of tmp) is at least 2n,
  373. * i.e. k is at least 2. There would have been nothing to
  374. * stop a user calling this function with k=1, but if they
  375. * did then we wouldn't have got to _here_ in the code -
  376. * we would have noticed above that all ominoes were
  377. * already at their target sizes, and terminated :-)
  378. */
  379. assert(wh >= 2*n);
  380. for (i = 0; i < n; i++)
  381. tmp[2*i] = tmp[2*i+1] = -1;
  382. qhead = qtail = 0;
  383. queue[qtail++] = j;
  384. tmp[2*j] = tmp[2*j+1] = -2; /* special value: `starting point' */
  385. while (qhead < qtail) {
  386. int tmpsq;
  387. j = queue[qhead];
  388. /*
  389. * We wish to expand omino j. However, we might have
  390. * got here by omino j having a square stolen from it,
  391. * so first of all we must temporarily mark that
  392. * square as not belonging to j, so that our adjacency
  393. * calculations don't assume j _does_ belong to us.
  394. */
  395. tmpsq = tmp[2*j+1];
  396. if (tmpsq >= 0) {
  397. assert(own[tmpsq] == j);
  398. own[tmpsq] = -3;
  399. }
  400. /*
  401. * OK. Now begin by seeing if we can find any
  402. * unclaimed square into which we can expand omino j.
  403. * If we find one, the entire bfs terminates.
  404. */
  405. for (i = 0; i < wh; i++) {
  406. int dir;
  407. if (own[order[i]] != -1)
  408. continue; /* this square is claimed */
  409. /*
  410. * Special case: if our current omino was size 1
  411. * and then had a square stolen from it, it's now
  412. * size zero, which means it's valid to `expand'
  413. * it into _any_ unclaimed square.
  414. */
  415. if (sizes[j] == 1 && tmpsq >= 0)
  416. break; /* got one */
  417. /*
  418. * Failing that, we must do the full test for
  419. * addability.
  420. */
  421. for (dir = 0; dir < 4; dir++)
  422. if (addable[order[i]*4+dir] == j) {
  423. /*
  424. * We know this square is addable to this
  425. * omino with the grid in the state it had
  426. * at the top of the loop. However, we
  427. * must now check that it's _still_
  428. * addable to this omino when the omino is
  429. * missing a square. To do this it's only
  430. * necessary to re-check addremcommon.
  431. */
  432. if (!addremcommon(w, h, order[i]%w, order[i]/w,
  433. own, j))
  434. continue;
  435. break;
  436. }
  437. if (dir == 4)
  438. continue; /* we can't add this square to j */
  439. break; /* got one! */
  440. }
  441. if (i < wh) {
  442. i = order[i];
  443. /*
  444. * Restore the temporarily removed square _before_
  445. * we start shifting ownerships about.
  446. */
  447. if (tmpsq >= 0)
  448. own[tmpsq] = j;
  449. /*
  450. * We are done. We can add square i to omino j,
  451. * and then backtrack along the trail in tmp
  452. * moving squares between ominoes, ending up
  453. * expanding our starting omino by one.
  454. */
  455. #ifdef DIVVY_DIAGNOSTICS
  456. printf("(%d,%d)", i%w, i/w);
  457. #endif
  458. while (1) {
  459. own[i] = j;
  460. #ifdef DIVVY_DIAGNOSTICS
  461. printf(" -> %d", j);
  462. #endif
  463. if (tmp[2*j] == -2)
  464. break;
  465. i = tmp[2*j+1];
  466. j = tmp[2*j];
  467. #ifdef DIVVY_DIAGNOSTICS
  468. printf("; (%d,%d)", i%w, i/w);
  469. #endif
  470. }
  471. #ifdef DIVVY_DIAGNOSTICS
  472. printf("\n");
  473. #endif
  474. /*
  475. * Increment the size of the starting omino.
  476. */
  477. sizes[j]++;
  478. /*
  479. * Terminate the bfs loop.
  480. */
  481. break;
  482. }
  483. /*
  484. * If we get here, we haven't been able to expand
  485. * omino j into an unclaimed square. So now we begin
  486. * to investigate expanding it into squares which are
  487. * claimed by ominoes the bfs has not yet visited.
  488. */
  489. for (i = 0; i < wh; i++) {
  490. int dir, nj;
  491. nj = own[order[i]];
  492. if (nj < 0 || tmp[2*nj] != -1)
  493. continue; /* unclaimed, or owned by wrong omino */
  494. if (!removable[order[i]])
  495. continue; /* its omino won't let it go */
  496. for (dir = 0; dir < 4; dir++)
  497. if (addable[order[i]*4+dir] == j) {
  498. /*
  499. * As above, re-check addremcommon.
  500. */
  501. if (!addremcommon(w, h, order[i]%w, order[i]/w,
  502. own, j))
  503. continue;
  504. /*
  505. * We have found a square we can use to
  506. * expand omino j, at the expense of the
  507. * as-yet unvisited omino nj. So add this
  508. * to the bfs queue.
  509. */
  510. assert(qtail < n);
  511. queue[qtail++] = nj;
  512. tmp[2*nj] = j;
  513. tmp[2*nj+1] = order[i];
  514. /*
  515. * Now terminate the loop over dir, to
  516. * ensure we don't accidentally add the
  517. * same omino twice to the queue.
  518. */
  519. break;
  520. }
  521. }
  522. /*
  523. * Restore the temporarily removed square.
  524. */
  525. if (tmpsq >= 0)
  526. own[tmpsq] = j;
  527. /*
  528. * Advance the queue head.
  529. */
  530. qhead++;
  531. }
  532. if (qhead == qtail) {
  533. /*
  534. * We have finished the bfs and not found any way to
  535. * expand omino j. Panic, and return failure.
  536. *
  537. * FIXME: or should we loop over all ominoes before we
  538. * give up?
  539. */
  540. #ifdef DIVVY_DIAGNOSTICS
  541. printf("FAIL!\n");
  542. #endif
  543. retdsf = NULL;
  544. goto cleanup;
  545. }
  546. }
  547. #ifdef DIVVY_DIAGNOSTICS
  548. {
  549. int x, y;
  550. printf("SUCCESS! Final grid:\n");
  551. for (y = 0; y < h; y++) {
  552. for (x = 0; x < w; x++)
  553. printf("%3d", own[y*w+x]);
  554. printf("\n");
  555. }
  556. }
  557. #endif
  558. /*
  559. * Construct the output dsf.
  560. */
  561. for (i = 0; i < wh; i++) {
  562. assert(own[i] >= 0 && own[i] < n);
  563. tmp[own[i]] = i;
  564. }
  565. retdsf = dsf_new(wh);
  566. for (i = 0; i < wh; i++) {
  567. dsf_merge(retdsf, i, tmp[own[i]]);
  568. }
  569. /*
  570. * Construct the output dsf a different way, to verify that
  571. * the ominoes really are k-ominoes and we haven't
  572. * accidentally split one into two disconnected pieces.
  573. */
  574. tmpdsf = dsf_new(wh);
  575. for (y = 0; y < h; y++)
  576. for (x = 0; x+1 < w; x++)
  577. if (own[y*w+x] == own[y*w+(x+1)])
  578. dsf_merge(tmpdsf, y*w+x, y*w+(x+1));
  579. for (x = 0; x < w; x++)
  580. for (y = 0; y+1 < h; y++)
  581. if (own[y*w+x] == own[(y+1)*w+x])
  582. dsf_merge(tmpdsf, y*w+x, (y+1)*w+x);
  583. for (i = 0; i < wh; i++) {
  584. j = dsf_canonify(retdsf, i);
  585. assert(dsf_equivalent(tmpdsf, j, i));
  586. }
  587. cleanup:
  588. /*
  589. * Free our temporary working space.
  590. */
  591. sfree(order);
  592. sfree(tmp);
  593. dsf_free(tmpdsf);
  594. sfree(own);
  595. sfree(sizes);
  596. sfree(queue);
  597. sfree(addable);
  598. sfree(removable);
  599. /*
  600. * And we're done.
  601. */
  602. return retdsf;
  603. }
  604. DSF *divvy_rectangle(int w, int h, int k, random_state *rs)
  605. {
  606. DSF *ret;
  607. do {
  608. ret = divvy_rectangle_attempt(w, h, k, rs);
  609. } while (!ret);
  610. return ret;
  611. }