codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
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()
Private
[
?
]
Run code
Submit