Skip to content

Instantly share code, notes, and snippets.

@poshan0126
Created June 30, 2019 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poshan0126/16185aa7cd1c212840988ac72cc64340 to your computer and use it in GitHub Desktop.
Save poshan0126/16185aa7cd1c212840988ac72cc64340 to your computer and use it in GitHub Desktop.
This Gist consist of a recursive iterative solution to the 8 Queen problem
def Permute(queens, row):
for i in range(8):
queens[row] = i
if Fine(queens, row):
if row == 7:
print(' ',queens)
globals()["solutions"] = globals()["solutions"] + 1
else:
Permute(queens, row+1)
def Fine(queens, row):
c = 0
derga = True
for i in range(row):
c, cur, oth = c+1, queens[row], queens[row-i-1]
if (cur == oth) or (cur-c == oth) or (cur+c == oth):
derga = False
break
return(derga)
print('The different solutions to 8 queen problem are:')
globals()["solutions"] = 0
queens = [20, 20, 20, 20, 20, 20, 20, 20]
for i in range(8):
queens[0] = i
Permute(queens, 1)
print('The total number of solutions is:',solutions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment