Questions tagged [n-queens]

The N-queens puzzle is a classic computer science problem that predates computer science. Given an N x N chessboard, the question asks the number of ways of placing N queens on the chessboard such that no queens attack any other queens. On a standard chessboard there are 92 solutions, of which 12 are rotationally invariant.

The N -queens puzzle (canonically, the 8 queens puzzle) is a famous and classic computer science problem due to its challenging nature, and its ability to be solved through the application of recursive backtracking. It is often used to introduce the concept of backtracking.

Background

The N-queens problem predates computers, and was first proposed and solved by chess players in the 19th century. The problem asks, given an N x N chessboard, how many unique ways are there of placing N queens on the board such that no queen attacks any other queen. The queen can travel vertically, horizontally, and diagonally, so as each queen is placed, the number of remaining legal positions becomes smaller and smaller.

Solution Procedure

A sensible recursive backtracking solution to the N-queens problem places one queen at a time, restricting the solution space considered for further queen placements only to legal positions (contrast with a brute force solution, which waits until all N queens have been placed to check if the placement of each queen is valid).

Variations

There are variations on the N queens problem - for example, the N rooks problem (placing N rooks on an N x N chessboard such that no rook attacks any other rook), which is a slightly less constrained version of the N queens problem, as rooks can attack vertically and horizontally, but not diagonally. There are other variations involving knights, bishops, and kings, but these pieces typically involve placing more than N pieces to keep the problem from becoming trivial.

Mathematicians and computer scientists have published diverse investigations into solving the N-queens puzzle in a variety of contexts: three dimensional chessboards, torroidal chessboards, and extremely large chessboards.

Useful Resources

Resources Explaining N-Queens Problem

N-Queens Solutions in Code

350 questions
5
votes
5 answers

Algorithm of N queens

Algorithm NQueens ( k, n) //Prints all Solution to the n-queens problem { for i := 1 to n do { if Place (k, i) then { x[k] := i; if ( k = n) then write ( x [1 : n] else NQueens ( k+1, n); …
user2967440
  • 131
  • 2
  • 2
  • 9
5
votes
1 answer

Eight Queens Heuristic

I am developing a heuristic to place 8 queens on 8x8 chessboard. each square has its own elimination number (to indicate how many squares of an empty chessboard are “eliminated” if a queen is placed in that square.) and the each queen should be…
kzidane
  • 753
  • 3
  • 11
  • 29
4
votes
1 answer

Poor performance on the nqueens min-conflic search

I am implementing the nqueens min-conflict search as mentioned by Norvig, S., & Peter, J. R. and. (2014). Artificial Intelligence A Modern Approach. In Pearson (Vol. 58, Issue 12). The authors mention this heuristic alone is super efficient: Yet…
Just_Alex
  • 518
  • 1
  • 4
  • 16
4
votes
1 answer

Why is my N Queens algorithm reaching the last row?

I think I understand the point of the NQueens algorithm - you check each row for available spaces and if they exist, put a queen there, and recursively call the algorithm to the next row. Here is my algorithm (with N size 3) from typing import…
BigBoy1337
  • 4,735
  • 16
  • 70
  • 138
4
votes
1 answer

How to solve the 8 queens problem with CVXPY?

I am really new to CVXPY. I am trying to solve the 8 queens problem, i.e., the problem of placing 8 chess queens on an 8 x 8 chessboard so that no two queens threaten each other. As I understand it, the constrainsts should be: sum of each row…
4
votes
6 answers

Solving the n-queen puzzle

I have just solved the nqueen problem in python. The solution outputs the total number of solutions for placing n queens on an nXn chessboard but trying it with n=15 takes more than an hour to get an answer. Can anyone take a look at the code and…
user559611
4
votes
3 answers

Queens on chessboard solved randomly in Python

The idea is to try solve the "queen problem" by totally placing the queens totally random in each row of the chess board, and see how many repetitions it takes to solve it. The chess board can be any size. My idea is to create s lists, each…
kjubus
  • 443
  • 3
  • 8
  • 21
4
votes
2 answers

How could I modify the array elements recursively with backtracking?

I wrote a kind of N-Queens algorithm, handling only the vertical and horizontal threats detection. Thus, it's rather a N-Towers solutions finder. To do this, I use recursion. It's a well-known algorithm. For each square of the chessboard, I place a…
JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70
4
votes
1 answer

Finding Multiple Solutions to "8 Queens"

Some of you computer science, mathematic, etc... .majors might have experience with this problem. It is famously known as the 8 Queens. Essentially, how many different ways can you place 8 queens on an 8x8 chess board so that none of them conflict…
Trevor
  • 1,284
  • 3
  • 15
  • 33
4
votes
3 answers

N-queens puzzle in Java with 1D array

I am working a problem that seems to be somewhat famous among beginning programmers, the 8 queens puzzle. I have seen several solutions to this problems using 2D arrays, recursion etc, but this problem is an assignment given in CS course book…
Esben86
  • 483
  • 8
  • 21
4
votes
3 answers

N Queens Puzzle - Where is the Backtracking in this solution?

While studying the well known N Queens puzzle, I came across this straightforward and easy to understand implementation in C: #include #include int board[20],count; int main() { int n,i,j; void queen(int row,int n); printf(" -…
Jay Souper
  • 2,576
  • 2
  • 14
  • 17
4
votes
1 answer

Getting Stuck On the Algorithm for q bishops on an n*n chessboard

I'm using C++, but my question is more about algorithms than implementation. The problem is the following: Write a program that inputs two integers n and k, where n>=k. Your program should calculate the number of different ways that k bishops could…
4
votes
4 answers

How to Solve N-Queens in Scheme?

I'm stuck on the extended exercise 28.2 of How to Design Programs. I used a vector of true or false values to represent the board instead of using a list. This is what I've got which doesn't work: #lang Scheme (define-struct posn (i j)) ;takes…
Philip
  • 41
  • 1
  • 2
4
votes
3 answers

Puzzled about backtracking in Eight Queen

I have a difficult time understanding recursion and backtracking, albeit I have done some simple exercises (e.g. Fibonacci). So allow me to present my "brain flow" here: I read the textbook and know that you can use backtracking to remove the…
Nicholas Humphrey
  • 1,220
  • 1
  • 16
  • 33
4
votes
2 answers

Scheme, N-queens optimization strategies SICP chapter 2

SICP contains an partially complete example of the n-queens solutions, by walking a tree of every possible queen placement in the last row, generating more possible positions in the next row to combine the results so far, filtering the possibilities…
WorBlux
  • 1,423
  • 11
  • 20
1
2
3
23 24