Skip to content

Commit 78ac33d

Browse files
Merge pull request steam-bell-92#1090 from Saloni885/main
👑 Added N-Queens Problem Solver (Console + Web App)
2 parents d84a3ac + 9dba9ab commit 78ac33d

3 files changed

Lines changed: 2768 additions & 6 deletions

File tree

utilities/N-Queens/N-Queens.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def print_board(board, n):
2+
for row in board:
3+
print(" ".join("♛" if col else "⬜" for col in row))
4+
print()
5+
6+
def is_safe(board, row, col, n):
7+
# Check column
8+
for i in range(row):
9+
if board[i][col]:
10+
return False
11+
# Check upper-left diagonal
12+
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
13+
if board[i][j]:
14+
return False
15+
# Check upper-right diagonal
16+
for i, j in zip(range(row, -1, -1), range(col, n)):
17+
if board[i][j]:
18+
return False
19+
return True
20+
21+
def solve(board, row, n, solutions):
22+
if row == n:
23+
solutions.append([r[:] for r in board])
24+
return
25+
for col in range(n):
26+
if is_safe(board, row, col, n):
27+
board[row][col] = 1
28+
solve(board, row + 1, n, solutions)
29+
board[row][col] = 0
30+
31+
def main():
32+
n = int(input("Enter board size (n): "))
33+
board = [[0]*n for _ in range(n)]
34+
solutions = []
35+
solve(board, 0, n, solutions)
36+
37+
print(f"Total solutions for {n}-Queens: {len(solutions)}\n")
38+
for sol in solutions:
39+
print_board(sol, n)
40+
41+
if __name__ == "__main__":
42+
main()

web-app/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,19 @@ <h2 class="playground-title">🐍 Python Playground</h2>
574574
</p>
575575
</div>
576576

577+
<div class="project-card" data-project="nqueens" data-category="utilities">
578+
<div class="card-icon">👑</div>
579+
<h3>N-Queen Problem Solver</h3>
580+
<p>Place queens safely on the board using backtracking.</p>
581+
<button class="btn-play">Try It</button>
582+
</div>
583+
584+
585+
<div class="project-card" data-category="utilities" data-project="tower-of-hanoi">
586+
<div class="card-icon">🗼</div>
587+
<h3>Tower of Hanoi</h3>
588+
<p>Solve the classic puzzle!</p>
589+
<button class="btn-play">Try It</button>
577590
<div class="playground-body">
578591
<div class="editor-panel">
579592
<div class="panel-header">

0 commit comments

Comments
 (0)