Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Place Queens by TovarischZhukov
COLS="abcdefgh"
ROWS="12345678"
def add(retval,i,j):
if 0<=i<8 and 0<=j<8: retval.add("%s%s"%(COLS[i],ROWS[j]))
def check(pos):
retval=set()
posi=COLS.find(pos[0])
posj=ROWS.find(pos[1])
for k in range(8):
add(retval,posi,k)
add(retval,k,posj)
add(retval,posi+k,posj+k)
add(retval,posi-k,posj-k)
add(retval,posi+k,posj-k)
add(retval,posi-k,posj+k)
return retval
def place_queens(placed):
if len(placed)==8: return placed
checked=set()
new_placed=set(placed)
for val in placed:
new_placed.remove(val)
el=check(val)
if el.intersection(new_placed):
return set()
checked=checked.union(el)
for i in COLS:
for j in ROWS:
val="%s%s"%(i,j)
if val not in checked:
new_placed=set(placed)
new_placed.add(val)
r=place_queens(new_placed)
if r: return r
return set()
Jan. 25, 2016