forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueen.py
More file actions
39 lines (33 loc) · 893 Bytes
/
nqueen.py
File metadata and controls
39 lines (33 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 27 16:23:14 2020
@author: Ahtazaz
"""
n = int(input("Enter the number of Queens that you want: "))
Queen = [i for i in range(n)]
def place(i, j):
for k in range(i):
if (Queen[k] == j or abs(Queen[k]-j) == abs(i-k)):
return False
return True
def print_queen():
print("\nPlaces of Queens\n|", end="")
for i in range(n):
for j in range(Queen[i]):
print(" |", end="")
print("Q |", end="")
for j in range(n-Queen[i]-1):
print(" |", end="")
if(not i == n-1):
print("\n|", end="")
print()
def n_queen(i):
if i<n:
for j in range(n):
if(place(i, j)):
Queen[i] = j
if (i == n-1):
print_queen()
else:
n_queen(i+1)
n_queen(0)