-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_Queen.py
151 lines (105 loc) · 3.26 KB
/
N_Queen.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pygame
import time
N = int(input("Enter size:"))
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
def draw(board):
for i in range(N):
for j in range(N):
if board[j][i] == 0:
pygame.draw.rect(screen, white, [i*50, j*50, 50, 50])
pygame.draw.rect(screen, black, [i*50, j*50, 50, 50], 1)
if board[j][i] == 1:
pygame.draw.rect(screen, black, [i*50, j*50, 50, 50])
if board[j][i] == 2:
pygame.draw.rect(screen, red, [i*50, j*50, 50, 50])
pygame.draw.rect(screen, black, [i*50, j*50, 50, 50], 1)
pygame.display.update()
def drawfinal(board):
screen.fill(white)
for i in range(N):
for j in range(N):
if board[j][i] == 1:
pygame.draw.rect(screen, black, [i*50, j*50, 50, 50])
else:
pygame.draw.rect(screen, black, [i*50, j*50, 50, 50], 1)
pygame.display.update()
def printSolution(board):
for i in range(N):
for j in range(N):
print(board[i][j], end=" ")
print()
draw(board)
pygame.time.Clock().tick(5)
def isSafe(board, row, col):
for i in range(col):
if board[row][i] == 1:
for j in range(col):
if board[row][j] == 2:
board[row][j] = 0
return False
board[row][i] = 2
draw(board)
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 1:
for x, y in zip(range(row, -1, -1), range(col, -1, -1)):
if board[x][y] == 2:
board[x][y] = 0
return False
board[i][j] = 2
draw(board)
pygame.time.Clock().tick(5)
for i, j in zip(range(row, N, 1), range(col, -1, -1)):
if board[i][j] == 1:
for x, y in zip(range(row, N, 1), range(col, -1, -1)):
if board[x][y] == 2:
board[x][y] = 0
return False
board[i][j] = 2
draw(board)
pygame.time.Clock().tick(5)
return True
def placeQueen(board, col):
if col == N:
return True
for i in range(N):
if isSafe(board, i, col):
board[i][col] = 1
draw(board)
pygame.time.Clock().tick(5)
if placeQueen(board, col + 1) == True:
return True
board[i][col] = 0
draw(board)
pygame.time.Clock().tick(5)
return False
def solveNQueen(board):
if placeQueen(board, 0) == False:
print("Solution does not exist")
return False
printSolution(board)
return True
def main():
pygame.init()
global screen
size = N * 50
screen = pygame.display.set_mode((size, size))
screen.fill(white)
pygame.display.update()
run = True
done = False
board = [[0] * N for _ in range(N)]
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if done == False:
if solveNQueen(board):
break
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
drawfinal(board)
main()