Last active
September 4, 2017 20:24
-
-
Save icsaba/6489cc4b89350097d46f6b22f9dc0b6b to your computer and use it in GitHub Desktop.
n-queens problem in python
This file contains 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 sys | |
import itertools | |
import time | |
class QueenContainer: | |
def __init__(self): | |
self.x = tuple() | |
self.y = tuple() | |
self.d1 = tuple() | |
self.d2 = tuple() | |
self.queens = {} | |
self.n = 100 | |
def in_row(self, x): | |
return x in self.x | |
def in_col(self, y): | |
return y in self.y | |
def in_diagonal(self, d1, d2): | |
return d1 in self.d1 or d2 in self.d2 | |
def reset_container(self): | |
self.x = tuple() | |
self.y = tuple() | |
self.d1 = tuple() | |
self.d2 = tuple() | |
self.queens = {} | |
return self | |
def put_queen(self, x, y): | |
d1, d2 = x + y, x - y | |
if not self.in_row(x) and not self.in_col(y) and not self.in_diagonal(d1, d2): | |
self.x += (x,) | |
self.y += (y,) | |
self.d1 += (d1,) | |
self.d2 += (d2,) | |
self.queens[(x, y)] = True | |
def put_n_queens(self): | |
coords = itertools.product(range(self.n), repeat=2) | |
for start in coords: | |
self.reset_container() | |
self.put_queen(*start) | |
points = itertools.product(range(self.n), repeat=2) | |
for point in points: | |
self.put_queen(*point) | |
if len(self.x) == self.n: | |
break | |
return self | |
def print_table(self): | |
print(self.queens) | |
for i in range(self.n): | |
for j in range(self.n): | |
if (j, i) in self.queens: | |
sys.stdout.write("1 ") | |
else: | |
sys.stdout.write("0 ") | |
print() | |
def set_queens_number(self, queen_number): | |
self.n = queen_number | |
return self | |
if __name__ == '__main__': | |
start_time = time.clock() | |
QueenContainer()\ | |
.set_queens_number(100)\ | |
.put_n_queens()\ | |
.print_table() | |
print(time.clock() - start_time, "seconds") |
This file contains 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 random | |
import sys | |
import itertools | |
queens, n = {}, 8 | |
def get_rows(x): | |
return (q for q in queens.keys() if q and q[0] == x) | |
def get_cols(y): | |
return (q for q in queens.keys() if q and q[1] == y) | |
def get_diagonals(d1, d2): | |
return (q for q in queens.keys() if q and (sum(q) == d1 or q[0] - q[1] == d2)) | |
def put_queen(x,y): | |
if not any(get_rows(x)) and not any(get_cols(y)) and not any(get_diagonals(x+y,x-y)): | |
queens[(x,y)] = True | |
def put_n_queens_randomly(): | |
global queens | |
coords = itertools.product(range(n), repeat=2) | |
while len(queens.keys()) < n: | |
queens = {} | |
random.shuffle(coords) | |
for point in coords: | |
put_queen(*point) | |
def put_n_queens(): | |
global queens | |
coords = itertools.product(range(n), repeat=2) | |
for start in coords: | |
queens = {} | |
put_queen(*start) | |
points = itertools.product(range(n), repeat=2) | |
for point in points: | |
put_queen(*point) | |
if len(queens.keys()) == n: | |
break | |
def print_table(): | |
for i in range(n): | |
for j in range(n): | |
if (i,j) in queens: | |
sys.stdout.write("1 ") | |
else: | |
sys.stdout.write("0 ") | |
print() | |
if __name__ == '__main__': | |
put_n_queens() | |
print_table() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment