Created
November 30, 2015 11:41
-
-
Save llighterr/830cee74c7913517b9c0 to your computer and use it in GitHub Desktop.
Expert System Lab 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
chars = {True: 'QQ|', False: '__|'} | |
def queens(x, y): | |
def go_deep(xs, ys, us, vs): | |
if len(xs) == 0: | |
return () | |
row = xs[0] | |
for col in ys: | |
u = row - col | |
v = row + col | |
if u in us and v in vs: | |
col_i = ys.index(col) | |
u_i = us.index(u) | |
v_i = vs.index(v) | |
queens = go_deep(xs[1:], ys[:col_i] + ys[col_i + 1:],\ | |
us[:u_i] + us[u_i + 1:], vs[:v_i] + vs[v_i + 1:]) | |
if queens is not None: | |
print_queens(queens) | |
print "" | |
return ((row, col),) + queens | |
return None | |
xs = range(8) | |
ys = range(8) | |
us = range(-7, 8) | |
vs = range(15) | |
us.remove(x - y) | |
vs.remove(x + y) | |
return ((x, y),) + go_deep(xs[:x] + xs[x + 1:], ys[:y] + ys[y + 1:], us, vs) | |
def print_queens(queens): | |
for i in range(8): | |
print '|' + reduce(operator.__add__, map(lambda p: chars[p in queens],\ | |
zip((i,) * 8, range(8))), "") | |
print "Queens" | |
x = int(raw_input("x>>>")) | |
y = int(raw_input("y>>>")) | |
if x not in range(8) or y not in range(8): | |
print "Coordinates must be in range [0:7]" | |
else: | |
print_queens(queens(x, y)) | |
raw_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment