Created
June 11, 2020 23:09
-
-
Save Youngestdev/e59f674bb52f46266f6810fad4a51bd8 to your computer and use it in GitHub Desktop.
Generating N-Queens possible solution sturvs.
This file contains hidden or 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 placequeens(arr, r): | |
| n = len(arr) | |
| res = [] | |
| if r == n: | |
| # Print solutions | |
| print(arr) | |
| else: | |
| for j in range(0, n): | |
| legal = True | |
| for i in range(0, r): | |
| if (arr[i] == j) or (arr[i] == j + r - i) or (arr[i] == j - r + i): | |
| legal = False | |
| if legal: | |
| arr[r] = j | |
| res.append(arr) | |
| placequeens(arr, r+1) | |
| return len(res) # return length of available solutions.. returns 8 for an 8 * 8 board sha. | |
| print(placequeens([0,0,0,0,0,0,0,0], 0)) | |
| print(placequeens([0,0,0,0], 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment