- commit
- 26e10585d1463720d39b38a5170ad842c42b4a0d
- parent
- eb6280f0b60a754663b5f026b2e6f0ae3cd80c6f
- author
- Dami <dami.etoile@gmail.com>
| M | takuzu/ | 65 | ++++++++++++++++++++++++++---- |
@@ -35,7 +35,6 @@ func fillGrid(g grid, size int) bool {
3535 }3636 3737 // canPlace checks whether placing val at (x,y) would violate Takuzu constraints.38-// It temporarily mutates g[y][x] to test row/column uniqueness, then restores it.3938 func canPlace(g grid, size, x, y int, val rune) bool {4039 // Check no three consecutive in row.4140 if x >= 2 && g[y][x-1] == val && g[y][x-2] == val {@@ -83,30 +82,78 @@ func canPlace(g grid, size, x, y int, val rune) bool {
8382 }8483 8584 // If this placement would complete the row, check uniqueness against other complete rows.86- g[y][x] = val87- if rowFilled(g, y, size) {85+ // The cell at (x,y) is empty, so the row is complete iff every other cell is filled.86+ if rowFilledExcept(g, y, x, size) {8887 for other := range size {89- if other != y && rowFilled(g, other, size) && rowEqual(g[y], g[other]) {90- g[y][x] = emptyCell88+ if other != y && rowFilled(g, other, size) && rowEqualWith(g[y], g[other], x, val) {9189 return false9290 }9391 }9492 }9593 9694 // If this placement would complete the column, check uniqueness against other complete columns.97- if colFilled(g, x, size) {95+ if colFilledExcept(g, x, y, size) {9896 for other := range size {99- if other != x && colFilled(g, other, size) && colEqual(g, size, x, other) {100- g[y][x] = emptyCell97+ if other != x && colFilled(g, other, size) && colEqualWith(g, size, x, other, y, val) {10198 return false10299 }103100 }104101 }105- g[y][x] = emptyCell106102 107103 return true108104 }109105 106+// rowFilledExcept returns true if every cell in row y is filled except column skip.107+func rowFilledExcept(g grid, y, skip, size int) bool {108+ for x := range size {109+ if x != skip && g[y][x] == emptyCell {110+ return false111+ }112+ }113+ return true114+}115+116+// colFilledExcept returns true if every cell in column x is filled except row skip.117+func colFilledExcept(g grid, x, skip, size int) bool {118+ for y := range size {119+ if y != skip && g[y][x] == emptyCell {120+ return false121+ }122+ }123+ return true124+}125+126+// rowEqualWith compares rows a and b, treating a[overrideIdx] as overrideVal.127+func rowEqualWith(a, b []rune, overrideIdx int, overrideVal rune) bool {128+ if len(a) != len(b) {129+ return false130+ }131+ for i := range a {132+ av := a[i]133+ if i == overrideIdx {134+ av = overrideVal135+ }136+ if av != b[i] {137+ return false138+ }139+ }140+ return true141+}142+143+// colEqualWith compares columns c1 and c2, treating g[overrideRow][c1] as overrideVal.144+func colEqualWith(g grid, size, c1, c2, overrideRow int, overrideVal rune) bool {145+ for r := range size {146+ v1 := g[r][c1]147+ if r == overrideRow {148+ v1 = overrideVal149+ }150+ if v1 != g[r][c2] {151+ return false152+ }153+ }154+ return true155+}156+110157 func rowFilled(g grid, y, size int) bool {111158 for x := range size {112159 if g[y][x] == emptyCell {