-
- for (pass = 0; pass < 2; pass++) {
-
- x1 = i1 % W;
- y1 = i1 / W;
- x2 = i2 % W;
- y2 = i2 / W;
-
- do {
- /* Mark this edge. */
- if (pass == 0) {
- state->errors[min(y1,y2)*W+min(x1,x2)] ^=
- ERR_SQUARE_TMP;
- } else {
- state->errors[min(y1,y2)*W+min(x1,x2)] |=
- ERR_SQUARE;
- state->errors[min(y1,y2)*W+min(x1,x2)] &=
- ~ERR_SQUARE_TMP;
- }
-
- /*
- * Progress to the next edge by turning as
- * sharply right as possible. In fact we do
- * this by facing back along the edge and
- * turning _left_ until we see an edge we
- * can follow.
- */
- dx = x1 - x2;
- dy = y1 - y2;
-
- for (i = 0; i < 4; i++) {
- /*
- * Rotate (dx,dy) to the left.
- */
- dt = dx; dx = dy; dy = -dt;
-
- /*
- * See if (x2,y2) has an edge in direction
- * (dx,dy).
- */
- if (x2+dx < 0 || x2+dx >= W ||
- y2+dy < 0 || y2+dy >= H)
- continue; /* off the side of the grid */
- /* In the second pass, ignore unmarked edges. */
- if (pass == 1 &&
- !(state->errors[(y2-(dy<0))*W+x2-(dx<0)] &
- ERR_SQUARE_TMP))
- continue;
- if (state->soln[(y2-(dy<0))*w+x2-(dx<0)] ==
- (dx==dy ? -1 : +1))
- break;
- }
-
- /*
- * In pass 0, we expect to have found
- * _some_ edge we can follow, even if it
- * was found by rotating all the way round
- * and going back the way we came.
- *
- * In pass 1, because we're removing the
- * mark on each edge that allows us to
- * follow it, we expect to find _no_ edge
- * we can follow when we've come all the
- * way round the loop.
- */
- if (pass == 1 && i == 4)
- break;
- assert(i < 4);
-
- /*
- * Set x1,y1 to x2,y2, and x2,y2 to be the
- * other end of the new edge.
- */
- x1 = x2;
- y1 = y2;
- x2 += dx;
- y2 += dy;
- } while (y2*W+x2 != i2);
-
- }
-
- } else
- dsf_merge(dsf, i1, i2);
+ dsf_merge(dsf, y*W+x, W*h + y*w+x);
+ dsf_merge(dsf, y*W+(x+1), W*h + (y+1)*w+x);
+ }
+ if (state->soln[y*w+x] <= 0) {
+ /*
+ * There isn't a / in this square, so we can unify
+ * the top edge with the right, and the bottom
+ * with the left.
+ */
+ dsf_merge(dsf, y*W+x, W*h + (y+1)*w+x);
+ dsf_merge(dsf, y*W+(x+1), W*h + y*w+x);
+ }
+ }
+ /* Now go through again and mark the appropriate edges as erroneous. */
+ for (y = 0; y < h; y++)
+ for (x = 0; x < w; x++) {
+ int erroneous = 0;
+ if (state->soln[y*w+x] > 0) {
+ /*
+ * A / separates the top and left edges (which
+ * must already have been identified with each
+ * other) from the bottom and right (likewise).
+ * Hence it is erroneous if and only if the top
+ * and right edges are nonequivalent.
+ */
+ erroneous = (dsf_canonify(dsf, y*W+(x+1)) !=
+ dsf_canonify(dsf, W*h + y*w+x));
+ } else if (state->soln[y*w+x] < 0) {
+ /*
+ * A \ separates the top and right edges (which
+ * must already have been identified with each
+ * other) from the bottom and left (likewise).
+ * Hence it is erroneous if and only if the top
+ * and left edges are nonequivalent.
+ */
+ erroneous = (dsf_canonify(dsf, y*W+x) !=
+ dsf_canonify(dsf, W*h + y*w+x));
+ }
+ if (erroneous) {
+ state->errors[y*W+x] |= ERR_SQUARE;
+ err = TRUE;
+ }