```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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 ``` ```from array import array SIDE = 5 SQR_SIDE = SIDE * SIDE circuit = array("l", [0]) * SQR_SIDE nsolutions = 0 movex = array("l", [-1,-2,-2,-1,+1,+2,+2,+1]) movey = array("l", [-2,-1,+1,+2,+2,+1,-1,-2]) shift = array("l", [x * SIDE + y for x,y in zip(movex, movey)]) def showCircuit(): print for x in xrange(SIDE): x_SIDE = x * SIDE for y in xrange(SIDE): if SQR_SIDE < 100: print "%02d " % circuit[x_SIDE + y], else: print "%03d " % circuit[x_SIDE + y], print def solve(nb, x, y, movex=movex, movey=movey, shift=shift): global nsolutions, SIDE, SQR_SIDE, circuit pos = x * SIDE + y circuit[pos] = nb if nb == SQR_SIDE: #showCircuit() nsolutions += 1 circuit[pos] = 0 return newx = x + -1 if newx >= 0 and newx < SIDE: newy = y + -2 if newy >= 0 and newy < SIDE and not circuit[pos + shift[0]]: solve(nb+1, newx, newy) newx = x + -2 if newx >= 0 and newx < SIDE: newy = y + -1 if newy >= 0 and newy < SIDE and not circuit[pos + shift[1]]: solve(nb+1, newx, newy) newx = x + -2 if newx >= 0 and newx < SIDE: newy = y + 1 if newy >= 0 and newy < SIDE and not circuit[pos + shift[2]]: solve(nb+1, newx, newy) newx = x + -1 if newx >= 0 and newx < SIDE: newy = y + 2 if newy >= 0 and newy < SIDE and not circuit[pos + shift[3]]: solve(nb+1, newx, newy) newx = x + 1 if newx >= 0 and newx < SIDE: newy = y + 2 if newy >= 0 and newy < SIDE and not circuit[pos + shift[4]]: solve(nb+1, newx, newy) newx = x + 2 if newx >= 0 and newx < SIDE: newy = y + 1 if newy >= 0 and newy < SIDE and not circuit[pos + shift[5]]: solve(nb+1, newx, newy) newx = x + 2 if newx >= 0 and newx < SIDE: newy = y + -1 if newy >= 0 and newy < SIDE and not circuit[pos + shift[6]]: solve(nb+1, newx, newy) newx = x + 1 if newx >= 0 and newx < SIDE: newy = y + -2 if newy >= 0 and newy < SIDE and not circuit[pos + shift[7]]: solve(nb+1, newx, newy) circuit[pos] = 0 def main(): print "Search for side=%d" % SIDE for x in xrange(SIDE): for y in xrange(SIDE): solve(1, x, y); print "\n%dx%d case, %d solutions." % (SIDE, SIDE, nsolutions) import psyco; psyco.full() main() ```