# Solve the eight queens problem
# Find all possible ways to arrange eight queens on a chess board
# so that no two queens are "en prise".
# The queens are not in positions to be taken by each other.
# Python lists
# https://www.w3schools.com/python/python_lists.asp
# List is a collection which is ordered and changeable. Allows duplicate members.
# Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
# Python functions
# https://www.w3schools.com/python/python_functions.asp
# A function is a block of code which only runs when it is called.
# bool(boolean) True False and or not abs int datetime
import datetime
start=datetime.datetime.now()
# position = from 0 to 7
q=[0,0,0,0,0,0,0,0]
# number of solutions found
n=0
def showPositions():
for i in range(len(q)):
print(q[i]+1,end=" ")
print()
def check():
for i in range(len(q)):
for j in range(i+1,len(q)):
if q[i]==q[j] or (j-i)==abs(q[j]-q[i]):
return False;
return True
# solution 1 (brute force)
for q[0] in range(len(q)):
for q[1] in range(len(q)):
for q[2] in range(len(q)):
for q[3] in range(len(q)):
for q[4] in range(len(q)):
for q[5] in range(len(q)):
for q[6] in range(len(q)):
for q[7] in range(len(q)):
if check()==True:
showPositions()
print()
print("Number of solutions=",n)
end=datetime.datetime.now()
print("Time:",end-start)
pause=input()