EX_03/level-2/n_queens/README.md

48 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# N-Queens
## What is the N-Queens Problem?
Imagine you have a chessboard and some queens (the most powerful pieces in chess). The N-Queens problem is like a puzzle where you need to place N queens on an N×N chessboard so that **none of them can attack each other**.
### What's a Queen in Chess?
A queen is the strongest piece in chess. She can move:
- **Horizontally** (left and right)
- **Vertically** (up and down)
- **Diagonally** (in any diagonal direction)
She can move as many squares as she wants in these directions!
## The Challenge
Let's say we want to solve the **4-Queens problem** (N=4):
- We have a 4×4 chessboard (16 squares total)
- We need to place 4 queens
- **NO queen can attack any other queen**
### Example: What Does "Attack" Mean?
If we put a queen at position (0,0) - that's the top-left corner - she can attack:
```
Q . . . ← Queen here attacks all positions marked with X
X . . .
X . . .
X . . .
```
```
Q X X X ← Queen attacks horizontally
. . . .
. . . .
. . . .
```
```
Q . . . ← Queen attacks diagonally
. X . .
. . X .
. . . X
```
So we can't put any other queen in any of those X positions!