Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions csp-nqueens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import numpy as np


# 3 constraints
# no queens on the same row
# no queens on the same col
# no queens on the same diagonal


def is_safe_row(board,n):
for row in range(n):
if sum(board[row,:]) > 1: # if there is a row with more than one queen, then its sum will be more than 1s
return False
return True

def is_safe_col(board,n):
for col in range(n):
if sum(board[:,col]) > 1 :
return False
return True

def is_safe_diag(board,n):
diags = []
for i in range(-n+1,n):
# if the index i is negative, take the upper ith diagonal
# if the index i is positive, the the lower ith diagonal
diags.append(board.diagonal(i))

diags.extend(board.diagonal(i) for i in range(n-1,-n,-1))
for x in diags:
if len(x)>1:
if sum (x)>1:
return False
return True


def nqueens (board, queen, n):
if is_safe_row(board,n) and is_safe_col(board,n) and is_safe_diag(board,n):
if board.sum()==n:
return True

for row in range (0,n):
board[row,queen] = 1
print(f'Queen: {row}')
if is_safe_row(board,n) and is_safe_col(board,n) and is_safe_diag(board,n):
if nqueens(board, queen+1,n):
return True
board[row,queen] = 0
else:
board[row,queen] = 0
return False


if __name__ == '__main__':
n = 4
board = np.zeros((n, n))
if nqueens(board, 0, n):
print(board)
else:
print('Cannot find a solution for ',n,' queens.')