Problem
The Magnet (“Magnets”) puzzle involves placing domino-shaped magnets (each with a + and - pole) into predefined paired slots on a rectangular grid so that all given numerical constraints are satisfied.
Board cells are partitioned into domino slots (horizontal or vertical). Each domino must be assigned either (+, -) or (-, +) according to orientation, or left unused (X X). Additional global constraints:
- Row / column counts: Arrays
top[],bottom[],left[],right[]specify how many+or-signs must appear in each column or row (with-1meaning unconstrained).top[j]counts+in columnj;bottom[j]counts-in columnj; similarly forleft[i](pluses in rowi) andright[i](minuses in rowi). - Adjacency: No two orthogonally adjacent cells may contain the same sign (
+next to+or-next to-). Diagonals are allowed to match. - Domino structure: Orientation matrix
rulesgives for each slot-end one ofL, R, T, B(left/right or top/bottom half); valid dominoes are(L,R)horizontally adjacent or(T,B)vertically adjacent pairs. - Each domino slot may be unused (both cells marked
X).
Goal: Assign polarities (or unused) to all slots so that all constraints hold; output one valid filled board (or report unsolvable).
Examples
Example 1
| |
Example 2
| |
Constraints
- Typical puzzle sizes:
M, N <= 8..10(practical backtracking limit) - Number of domino slots derived from
ruleslayout. - Sum feasibility: For each row/col with constraint
cfor+(or-), must have enough available cells in that row/col to reachc.
Solution
Method 1 - Backtracking with Incremental Constraint Pruning
Intuition
The search space is all assignments of each domino slot to one of three states: (+, -), (-, +) or unused (X, X). We prune early when:
- A row/col already exceeds its
+/-quota. - Remaining unfilled cells in a row/col are insufficient to reach a still-needed quota.
- Adjacency rule would be violated by placing a polarity at a cell.
By scanning the grid cell-by-cell in a deterministic order and branching only at canonical slot starts (L or T), we avoid duplicate work.
Approach
- Pre-parse
rulesto identify domino slot starts (cells markedLorT). - Maintain counters:
plusRow[i],minusRow[i],plusCol[j],minusCol[j].
- Recursive function
dfs(idx)whereidxenumerates domino slots:- For each slot (two coordinates
a,b), try states: (+, -)if both legal.(-, +)if both legal.X X(skip) always legal (but keep for solution existence; can be pruned if quotas require filling).- Before recursing, check row/col quota feasibility (no overfill; capacity remaining sufficient).
- For each slot (two coordinates
- Stop at first complete assignment satisfying all exact quotas (where specified; unconstrained quotas ignored).
- Use adjacency checks on-the-fly (constant time around the two modified cells).
Pseudocode
| |
Code
| |
| |
| |
Complexity
- ⏰ Time complexity:
O(3^S)in the worst case whereSis the number of domino slots (each slot has up to 3 choices). Pruning (quota feasibility + adjacency + early overfill) dramatically reduces search; practical boards solve quickly. - 🧺 Space complexity:
O(S + M*N)– recursion depth up to number of slots plus board & counter storage.