Knuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.
Tiling a board with the twelve pentominoes is an exact cover problem: a sparse 0/1 matrix whose columns are the 12 pieces and every board cell, and whose roughly 2,000 rows are every way to place one piece. Knuth's Algorithm X picks the column with the fewest live rows, tries each row in it, and recurses; dancing links stores the matrix as circular doubly linked lists so covering a column is a few pointer swaps, and because a removed node keeps its own pointers, backtracking splices it back in exactly reverse order. The search runs as an explicit-stack state machine with a per-frame budget, from a crawl you can follow piece by piece up to hundreds of thousands of link updates a second. On the right, covered column headers lift out of the header list with faint threads to the neighbors they will return to, the full matrix dims as rows are unlinked, and the chosen rows light up across it.
Try it. Pick a board below (6 x 10, 5 x 12, an 8 x 8 ring, a heart, or the notoriously tight 3 x 20) and change the speed with the minus and plus buttons. Click the board to pause, then hover a piece or a lit matrix row to see which column the search branched on and which option it is trying. Keys: 1 to 5 for boards, arrows for speed, space to pause, period to step one placement while paused, R to reshuffle.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build a pentomino puzzle solver that uses Knuth's Algorithm X with dancing links, visualized with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.
Start simple:
- Define the 12 pentominoes as small cell grids and generate every distinct rotation and reflection.
- Build the exact cover matrix for a 6 x 10 board: one column per piece and per cell (72), and one row per way to place a piece (its piece column plus the five cells it covers).
- Store it as dancing links: typed arrays L, R, U, D and C, a header node and a size count per column. Write cover(c) and uncover(c), where uncover undoes cover in exactly reverse order.
- Write Algorithm X as an explicit-stack loop (not recursion) with a step() function: choose the live column with the fewest rows, cover it, try each of its rows, and backtrack when a column runs out of rows.
- Call step() a few hundred times per frame and draw the currently placed pieces, each in its own color.
Once that works, make it beautiful:
- Draw each piece as one wooden block: inset only on its outer edges, rounded outer corners, a bevel with light top and left edges, and a soft drop shadow.
- Add a speed control from a few steps per second (newly placed pieces drop in, removed ones float away) up to as fast as the frame budget allows, and show link updates per second.
- Beside the board, draw the column header list as a row of nodes joined by their R links, with covered headers lifted out of the line and faint lines to the neighbors they still point to.
Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as other board shapes like a ring or a 3 x 20 strip, counting all 2,339 solutions with symmetry breaking, or a pause mode that shows which column each level branched on.