```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 ``` ```version(Tango) import tango.stdc.stdio; else import std.stdio; template Tuple(T...) { alias T Tuple; } alias Tuple!(-1,-2,-2,-1,+1,+2,+2,+1) movex; alias Tuple!(-2,-1,+1,+2,+2,+1,-1,-2) movey; const uint SIDE = 5; const uint SQR_SIDE = SIDE * SIDE; uint[SQR_SIDE] circuit; uint nsolutions; void showCircuit() { putchar('\n'); for (uint x; x < SIDE; x++) { for (uint y; y < SIDE; y++) if (SQR_SIDE < 100U) printf("%02d ", circuit[x * SIDE + y]); else printf("%03d ", circuit[x * SIDE + y]); putchar('\n'); } } void solve(uint nb, uint x, uint y) { uint newx, newy; circuit[x * SIDE + y] = nb; if (nb == SQR_SIDE) { version(doshow) showCircuit(); nsolutions++; circuit[x * SIDE + y] = 0; return; } foreach (uint i, sx; movex) { newx = x + sx; if (newx < SIDE) { newy = y + movey[i]; if (newy < SIDE && !circuit[newx * SIDE + newy]) solve(nb+1, newx, newy); } } circuit[x * SIDE + y] = 0; } void main() { printf("Search for side=%d\n", SIDE); for (uint x; x < SIDE; x++) for (uint y; y < SIDE; y++) solve(1U, x, y); fprintf(stderr, "\n%dx%d case, %d solutions.\n", SIDE, SIDE, nsolutions); } ```