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

301 questions
24
votes
9 answers

Solving N-Queens Problem... How far can we go?

The N-Queens Problem: This problem states that given a chess board of size N by N, find the different permutations in which N queens can be placed on the board without any one threatening each other. My question is: What is the maximum value of N…
user220751
16
votes
6 answers

Time complexity of N Queen using backtracking?

#include #include void printboard(int n); void fourQueen(int k,int n); int place(int k,int i); int x[100]; void NQueen(int k,int n) { int i; for(i=1;i<=n;i++) { if(place(k,i)==1) { x[k]=i; if(k==n) …
tanmoy
  • 1,283
  • 3
  • 19
  • 29
12
votes
2 answers

8-queen problem using Dynamic programming

I am quite confused with idea of implementing 8-queen problem using dynamic programming. It seems it is not possible at one end as for DP " if the problem was broken up into a series of subproblems and the optimal solution for each subproblem was…
mqpasta
  • 950
  • 3
  • 14
  • 36
12
votes
3 answers

How to use the Select monad to solve n-queens?

I'm trying to understand how the Select monad works. Apparently, it is a cousin of Cont and it can be used for backtracking search. I have this list-based solution to the n-queens problem: -- All the ways of extracting an element from a list. oneOf…
danidiaz
  • 24,322
  • 4
  • 41
  • 79
12
votes
2 answers

What is the best complexity of N-Queens puzzle?

Can the N-Queens puzzle theoretically be solved in polynomial time? If so, what is the best complexity of it? I have found many algorithms, but I haven't found what exactly the time complexity is. Are there any papers or documents giving an exact…
Rosetta
  • 121
  • 1
  • 4
10
votes
5 answers

8 queens problem using backtracking recurison

I've been working on the 8 queens problem but I got stuck. I don't want code. I would love guidance and directions in order to understand how to solve this problem myself using backtracking recursion. The program should enumerate all solutions to…
Nath
  • 229
  • 2
  • 4
  • 11
10
votes
5 answers

Hints to understand splendid program to solve Queens

In Art of Prolog of Sterling & Shapiro, exercise Section 14.1 (v): queens(N,Qs) :- length(Qs,N), place_queens(N,Qs,_,_). place_queens(0,_Qs,_Ups,_Downs). place_queens(I,Qs,Ups,[_|Downs]) :- I > 0, I1 is I-1, …
noein
  • 301
  • 1
  • 10
8
votes
1 answer

Parallel running of Racket code for N-Queens problem

I am using following simple code to solve n-queens problem: #lang racket ; following returns true if queens are on diagonals: (define (check-diagonals bd) (for/or ((r1 (length bd))) (for/or ((r2 (range (add1 r1) (length bd)))) (=…
rnso
  • 20,794
  • 19
  • 81
  • 167
8
votes
2 answers

N-Queens Symmetry Breaking Google OR Tools

One of the samples for the Google or-tools is a solver for the n-queens problem. At the bottom it says that the implementation can be improved by adding symmetry breaking constraints to the constraint solver. Looking around the internet, I found…
Nick Larsen
  • 17,643
  • 6
  • 62
  • 94
7
votes
2 answers

How do you test for diagonal in n-queens?

I'm studying the n-queen backtracker. Can someone explain to me how other_row_pos checks for diagonals? I'm not sure why it works or how it works. taken from wikibooks -…
runners3431
  • 1,375
  • 11
  • 26
7
votes
2 answers

How to use replicateM solving the eight queens problem?

I just started learning to code Haskell so apologies if this is a stupid question. I am trying to redo the eight queens problem by making use of the [] monad. Here is the code, import Control.Monad addqueen :: [Int] -> [[Int]] addqueen xs = [ x:xs…
user2812201
  • 447
  • 3
  • 7
6
votes
1 answer

All possible solution of the n-Queen's algorithm

When implementing an algorithm for all possible solution of an n-Queen problem, i found that the same solution is reached by many branches. Is there any good way to generate every unique solutions to the n-Queens problem? How to avoid the duplicate…
phoxis
  • 52,327
  • 12
  • 74
  • 110
6
votes
1 answer

Constrained N-Rook Number of Solutions

The purpose of this post is mainly for keywords for related researches. Unconstrained N-Rook Problem Count all possible arrangements of N rooks on an N by M (N<=M) chessboard so that no rooks are attacking each other. The solution is trivial:…
hychou
  • 411
  • 5
  • 14
6
votes
4 answers

N-Queens Solutions C++

So I need help with the classic N-Queens problem. The command to run the program will be: nqueens N k - where N is the size of the table (N x N) and k is the number of solutions So for example if I were to run the program by typing nqueens 4 1 the…
hbranum
  • 81
  • 1
  • 2
  • 10
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
1
2 3
20 21