Skip to content

Instantly share code, notes, and snippets.

@Youngestdev
Created June 11, 2020 23:09
Show Gist options
  • Select an option

  • Save Youngestdev/e59f674bb52f46266f6810fad4a51bd8 to your computer and use it in GitHub Desktop.

Select an option

Save Youngestdev/e59f674bb52f46266f6810fad4a51bd8 to your computer and use it in GitHub Desktop.
Generating N-Queens possible solution sturvs.
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