Created
June 30, 2019 11:50
-
-
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
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
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