codepad | about

 Link:    [ raw code | fork ]

Python, pasted on Jul 21:
 ```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 ``` ```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 for i in xrange(8): newx = x + movex[i] if newx >= 0 and newx < SIDE: newy = y + movey[i] if newy >= 0 and newy < SIDE and not circuit[pos + shift[i]]: 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() ```

Comments: