-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Nquuens Algorithm with Explaination and Lesser Complexity
- Loading branch information
1 parent
798d57c
commit 4054557
Showing
3 changed files
with
63 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
def solve_n_queens(n): | ||
if n < 1: | ||
return 0 | ||
|
||
res = [] | ||
dfs(n, 0, 0, 0, 0, [], res) | ||
return len(res) | ||
|
||
def dfs(n, row, cols, left_diagonal, right_diagonal, path, res): | ||
if row == n: | ||
res.append(path) | ||
return | ||
|
||
# Generate a bit mask for valid positions in the row | ||
valid_positions = ((1 << n) - 1) & (~(cols | left_diagonal | right_diagonal)) | ||
|
||
while valid_positions: | ||
# Get the position of the least significant bit | ||
pos = valid_positions & (-valid_positions) | ||
|
||
# Remove the least significant bit from the mask | ||
valid_positions &= valid_positions - 1 | ||
|
||
# Calculate the column index of the queen | ||
col = bin(pos - 1).count('1') | ||
|
||
# Generate the next row state | ||
new_cols = cols | pos | ||
new_left_diagonal = (left_diagonal | pos) << 1 | ||
new_right_diagonal = (right_diagonal | pos) >> 1 | ||
|
||
# Recurse on the next row | ||
dfs(n, row+1, new_cols, new_left_diagonal, new_right_diagonal, path+[col], res) | ||
|
||
if __name__ == "__main__": | ||
n = int(input("Enter the size of the board: ")) | ||
count = solve_n_queens(n) | ||
print("Number of valid solutions for {}-queens problem: {}".format(n, count)) | ||
|
||
# The complexity for my code is : time complexity of O(2^n) and a space complexity of O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The N-Queens problem is a classic puzzle where the goal is to place N chess queens on an NxN chessboard so that no two queens attack each other. In other words, no two queens can share the same row, column, or diagonal. | ||
|
||
Here's an example of how we can visualize the N-Queens problem in a game format with a 4x4 board and 4 queens: | ||
a b c d | ||
1 Q . . . | ||
2 . . Q . | ||
3 . Q . . | ||
4 . . . Q | ||
|
||
Each square on the board represents a position where a queen can be placed. In this example, we have placed four queens on the board so that no two queens share the same row, column, or diagonal. | ||
|
||
Now, let's say you have a specific input, such as a 5x5 board and 5 queens. Here's how we can visualize the game with this input: | ||
a b c d e | ||
1 . . Q . . | ||
2 Q . . . . | ||
3 . . . . Q | ||
4 . Q . . . | ||
5 . . . Q . | ||
|
||
|
||
|