Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N-queens problem using python #2508

Merged
merged 1 commit into from
Oct 5, 2022
Merged

Conversation

yashtriyar
Copy link
Contributor

@yashtriyar yashtriyar commented Oct 5, 2022

Problem

N queens Problem

Solution

def isSafe(row,col,board,n):
#vertical direction
i=row-1
while i>=0:
if board[i][col]==1:
return False
i-=1
i=row-1
j=col-1

while i>=0 and j>=0:
    if board[i][j]==1:
        return False
    i-=1
    j-=1
i=row-1
j=col+1
while i>=0 and j<n:
    if board[i][j]==1:
        return False
    i-=1
    j+=1

return True

def printPathsHelper(row,n,board):
if row==n:
for i in range(n):
for j in range(n):
print(board[i][j],end=' ')
print()
return
for col in range(n):
if isSafe(row,col,board,n) is True:
board[row][col]=1
printPathsHelper(row+1,n,board)
board[row][col]=0
return

def nQueen(n):
#Implement Your Code Here
board=[[0 for j in range(n)]for i in range(n)]
printPathsHelper(0,n,board)

n = int(input())
nQueen(n)

Changes proposed in this Pull Request :

  • 1.
  • ..

Other changes

@fineanmol fineanmol added the hacktoberfest-accepted Accept for hacktoberfest, will merge later label Oct 5, 2022
@fineanmol fineanmol merged commit 460f77d into fineanmol:master Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Accept for hacktoberfest, will merge later
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants