Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
algorithms-in-python/backtracking/n_queens_math.py at Python-3.9rc2 · coderjack/algorithms-in-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
coderjack
/
algorithms-in-python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
Python-3.9rc2
Breadcrumbs
algorithms-in-python
/
backtracking
/
n_queens_math.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
165 lines (123 loc) · 4.87 KB
Python-3.9rc2
Breadcrumbs
algorithms-in-python
/
backtracking
/
n_queens_math.py
Copy path
Top
File metadata and controls
Code
Blame
165 lines (123 loc) · 4.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
r"""
Problem:
The n queens problem is of placing N queens on a N * N chess board such that no queen
can attack any other queens placed on that chess board. This means that one queen
cannot have any other queen on its horizontal, vertical and diagonal lines.
Solution:
To solve this problem we will use simple math. First we know the queen can move in all
the possible ways, we can simplify it in this: vertical, horizontal, diagonal left and
diagonal right.
We can visualize it like this:
left diagonal = \
right diagonal = /
On a chessboard vertical movement could be the rows and horizontal movement could be
the columns.
In programming we can use an array, and in this array each index could be the rows and
each value in the array could be the column. For example:
. Q . . We have this chessboard with one queen in each column and each queen
. . . Q can't attack to each other.
Q . . . The array for this example would look like this: [1, 3, 0, 2]
. . Q .
So if we use an array and we verify that each value in the array is different to each
other we know that at least the queens can't attack each other in horizontal and
vertical.
At this point we have that halfway completed and we will treat the chessboard as a
Cartesian plane. Hereinafter we are going to remember basic math, so in the school we
learned this formula:
Slope of a line:
y2 - y1
m = ----------
x2 - x1
This formula allow us to get the slope. For the angles 45º (right diagonal) and 135º
(left diagonal) this formula gives us m = 1, and m = -1 respectively.
See::
https://www.enotes.com/homework-help/write-equation-line-that-hits-origin-45-degree-1474860
Then we have this another formula:
Slope intercept:
y = mx + b
b is where the line crosses the Y axis (to get more information see:
https://www.mathsisfun.com/y_intercept.html), if we change the formula to solve for b
we would have:
y - mx = b
And like we already have the m values for the angles 45º and 135º, this formula would
look like this:
45º: y - (1)x = b
45º: y - x = b
135º: y - (-1)x = b
135º: y + x = b
y = row
x = column
Applying this two formulas we can check if a queen in some position is being attacked
for another one or vice versa.
"""
from
typing
import
List
def
depth_first_search
(
possible_board
:
List
[
int
],
diagonal_right_collisions
:
List
[
int
],
diagonal_left_collisions
:
List
[
int
],
boards
:
List
[
List
[
str
]],
n
:
int
,
)
->
None
:
"""
>>> boards = []
>>> depth_first_search([], [], [], boards, 4)
>>> for board in boards:
... print(board)
['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
['. . Q . ', 'Q . . . ', '. . . Q ', '. Q . . ']
"""
""" Get next row in the current board (possible_board) to fill it with a queen """
row
=
len
(
possible_board
)
"""
If row is equal to the size of the board it means there are a queen in each row in
the current board (possible_board)
"""
if
row
==
n
:
"""
We convert the variable possible_board that looks like this: [1, 3, 0, 2] to
this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
"""
possible_board
=
[
". "
*
i
+
"Q "
+
". "
*
(
n
-
1
-
i
)
for
i
in
possible_board
]
boards
.
append
(
possible_board
)
return
""" We iterate each column in the row to find all possible results in each row """
for
col
in
range
(
n
):
"""
We apply that we learned previously. First we check that in the current board
(possible_board) there are not other same value because if there is it means
that there are a collision in vertical. Then we apply the two formulas we
learned before:
45º: y - x = b or 45: row - col = b
135º: y + x = b or row + col = b.
And we verify if the results of this two formulas not exist in their variables
respectively. (diagonal_right_collisions, diagonal_left_collisions)
If any or these are True it means there is a collision so we continue to the
next value in the for loop.
"""
if
(
col
in
possible_board
or
row
-
col
in
diagonal_right_collisions
or
row
+
col
in
diagonal_left_collisions
):
continue
""" If it is False we call dfs function again and we update the inputs """
depth_first_search
(
possible_board
+
[
col
],
diagonal_right_collisions
+
[
row
-
col
],
diagonal_left_collisions
+
[
row
+
col
],
boards
,
n
,
)
def
n_queens_solution
(
n
:
int
)
->
None
:
boards
=
[]
depth_first_search
([], [], [],
boards
,
n
)
""" Print all the boards """
for
board
in
boards
:
for
column
in
board
:
print
(
column
)
print
(
""
)
print
(
len
(
boards
),
"solutions were found."
)
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
n_queens_solution
(
4
)
You can’t perform that action at this time.