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

Add NQueen.py #580

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ United Kingdom - https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/
|[Darshit Dudhaiya](https://github.com/darshitdudhaiya) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I'm a Student and full-stack web developer and i enthusiast about coding. |
|[Naveen Kumar .v](https://github.com/NK-dev-24) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I'm a Student. |
|[Chandan Arya](https://github.com/alpha2lucifer) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I am a passionate developer with a keen interest in blockchain technology and decentralized applications. My focus lies in creating efficient and secure solutions, and I enjoy exploring various programming languages and frameworks to build innovative applications.|
|[Subhikshaa S](https://github.com/Subhikshaa23) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | A passionate web developer and a curious learner |


32 changes: 32 additions & 0 deletions Python/NQueen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#N-Queens Problem
def attack(i, j):
for k in range(0,N):
if board[i][k]==1 or board[k][j]==1:
return True
for k in range(0,N):
for l in range(0,N):
if (k+l==i+j) or (k-l==i-j):
if board[k][l]==1:
return True
return False

def N_queens(n):
if n==0:
return True
for i in range(0,N):
for j in range(0,N):
if (not(attack(i,j))) and (board[i][j]!=1):
board[i][j] = 1
if N_queens(n-1)==True:
return True
board[i][j] = 0
return False

print ("Enter the number of queens: ")
N = int(input())
board = [[0]*N for _ in range(N)]

N_queens(N)
print("Solution:")
for i in board:
print (i)