SPS Diffusion Beats Math's Best Snake Puzzle Records Without Brute Force
A diffusion model repurposed for combinatorics learns to draw maximal snake polyominoes in grids far beyond what brute force enumeration can reach.
ProTakeaways
- SPS Diffusion adapts denoising diffusion to generate maximal snake polyominoes without hardcoded rules.
- Architecture strips CLIP and VAE, keeps a 3-level mini U-Net with RoPE-2D self-attention.
- Trained on rectangles up to 50x14 using MSE loss on forward-diffused noise samples.
- Generates valid snakes up to 28x28, produces maximal candidates up to the 17x17 computational limit.
- Occasionally exceeds previously known hand-constructed lower bounds by up to 3 cells.
- Fails at 29x29 and larger, producing branching, cycles, or disconnected forests instead of snakes.
A team at Université du Québec à Trois-Rivières has taken the same class of model that powers image generators like Stable Diffusion and pointed it at an old open problem in combinatorics: finding the longest possible snake-shaped polyomino that fits inside a rectangle. Their model, called Structured Pixel Space Diffusion (SPS Diffusion), learns the rules of what makes a valid snake purely from examples, without any hardcoded constraints, and generates candidates in grids that are well beyond the reach of current brute force enumeration.
The combinatorics problem hiding inside a pixel grid
A polyomino is a shape formed by joining unit squares edge to edge. A snake polyomino is a special case where every cell has exactly two neighbors, except for two endpoint cells (the head and tail) that have one. A maximal snake is the longest such shape that fits inside a given rectangle. The problem of finding the length of maximal snake polyominoes in an arbitrary rectangle is still open.
The trouble is scale. Since there exists families of snake polyominoes that are in bijection with Fibonacci numbers, the number of snakes polyominoes grows exponentially with respect to their size, and the standard approach is to enumerate every possible snake and pick the longest. In brute force computations, researchers were able to reach sizes of up to 15x15 for squares, where the general pattern begins to emerge, but significantly larger rectangles remain out of reach of conventional methods. A recent improved enumeration by Barequet and Ben-Shachar using rotated bounding boxes pushed polyomino enumeration up to size 70, but the complexity is still exponential.

Why a diffusion model, and what got stripped out
Deep neural networks have polynomial complexity, which makes them theoretically faster than the current brute force algorithms used for generating snake polyominoes of maximal length. The authors chose a denoising diffusion probabilistic model (DDPM), the same family behind modern image generators, because a snake in a grid is essentially a binary image. But natural image diffusion models are massively overbuilt for this task, so they cut them down.
The result is a much leaner architecture:
- No CLIP encoder. There are no text prompts, so the text-image embedding module is unnecessary.
- No VAE and no latent space. Snake images are already very small, making such a compression step useless. Since there is no VAE, there is no latent space, thus only leaving the pixel space in which the polyomino images live.
- A mini U-Net. While a typical Stable Diffusion U-Net may employ 5 or 6 levels of downsampling, SPS Diffusion is restricted to only 3 levels to preserve structural fidelity.
- Rotary 2D positional embeddings (RoPE-2D) inside self-attention blocks, which encode relative spatial positions between cells.
The generation loop follows a standard DDPM recipe: start from Gaussian noise, iteratively predict and subtract noise, and finally round the continuous pixel values to 0 or 1 at the end. That final rounding is why intermediate steps show gray cells before the binary snake emerges.

Training on small grids, generating in bigger ones
The team trained on known maximal snakes using forward diffusion to produce noisy inputs, with mean squared error as the loss. The SPS Diffusion model was trained by computing the src (a noisy polyomino) using known maximal snakes for rectangles of dimension W less than or equal to 50 and H less than or equal to 14. They tried structural penalties like counting degree-three cells or disconnected components, but MSE was the only loss that reliably produced snake-like output, because early noisy intermediate images make structural checks unreliable.
Then came the interesting part: generalization beyond training sizes.
- Snake polyominoes were generated in squares for sizes up to 28x28, which is bigger than the maximum size used in training for squares (14x14).
- Valid snakes appeared in elongated rectangles up to 10x60.
- Maximal snakes were generated for rectangle sizes up to 17x17, which is the current computational limit for squares.
- Beyond that bound, the authors can estimate a lower bound on the minimal length of maximal snakes and, for several rectangle sizes, can sometimes exceed the lower bound by up to 3 cells.
That last point matters. The previously known lower bounds for maximal snake length were established by hand-constructed examples in the mathematical literature, and the network occasionally beats them. SPS Diffusion produced snakes whose lengths exceed the best previously reported lower bounds.

Where it breaks down
The model is far from perfect. As grids get larger, it starts producing forbidden shapes: cells with three neighbors (branching), closed loops with no head or tail (cycles), or disconnected pieces the authors call a forest of snakes. The error rate increases with the rectangle size, and the SPS Diffusion model makes fewer errors on elongated rectangles than on squares, since the corresponding snakes are more predictable and therefore easier to generate.
The wall is currently at 29x29 squares. For more than 25,000 images generated, no snakes were produced for a 29x29 square, only forests of snakes and forests of polyominoes. The authors suspect the fixed three-level U-Net simply lacks the receptive field to capture global structure at that scale, and that RoPE-2D positional encoding trained on smaller grids may lose effectiveness outside its training distribution.
What this actually gets you
Two takeaways stand out. First, more training data helps in a very direct way: adding squares up to 14x14 to the training set pushed the generation ceiling from 23x23 to 28x28, suggesting a self-training loop where the model's own outputs feed back in could keep extending the range. Second, this is a proof of concept that diffusion models can learn implicit combinatorial constraints, such as connectivity, degree limits, and maximality, from raw examples alone, with no symbolic rule-encoding.
The practical use here is not to replace exact enumeration for provably maximal snakes, but to generate strong candidates in rectangles where brute force is impossible, giving mathematicians conjectural bounds and structural patterns to investigate. The paper does not appear to release code or a trained model publicly, though the underlying integer sequence A331968 tracks the known values the model is trying to extend.