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
2
votes
2 answers

How do I detect if a queen is safe if I limit the queen's attack range to 5 squares?

I need to place N queen pieces on a MxM board so that no two queens attack each other. The difference from the original is that the queens can only attack up to 5 squares from their location, and not the whole row, column or diagonal as in the…
Leonardo Lopez
  • 1,113
  • 6
  • 20
  • 39
2
votes
1 answer

Understanding CLP(FD) Prolog code of N-queens problem

I am trying to understand N-queens problem's solution as given below: :- use_module(library(clpfd)). n_queens(N, Qs) :- length(Qs, N), Qs ins 1..N, safe_queens(Qs). safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), …
2
votes
3 answers

Prolog Programming

I have made two programs in Prolog for the nqueens puzzle using hill climbing and beam search algorithms. Unfortunately I do not have the experience to check whether the programs are correct and I am in dead end. I would appreciate if someone could…
user596970
  • 21
  • 1
  • 2
2
votes
1 answer

How recursion worked in n_queen Python

I'm trying to solve the classic n queen problem in python. Unfortunately the output was quite different with my expectation. I understood the back tracking algorithm but may be not clear on where I might make a mistake in writing the recursion. I…
evehsu
  • 39
  • 4
2
votes
4 answers

Pythonic way to check diagonals in nested list

I have a nested list of 0s and ones such as : L = [[1, 0, 1], [0, 0, 0], [0, 0, 1]] I want to check if any ones are diagonal to each other. The nested list can come in any size, as long as the length of the sublists is equal to the length…
Primusa
  • 13,136
  • 3
  • 33
  • 53
2
votes
1 answer

How to terminate backtracing recursion after first solution is found

I am trying to find the first solution of a Backtracking nQueen Algorithm. I want to terminate the execution of the code after I find first solution. But the program keeps running until all the solution is found. Here's my code: def…
Shadid
  • 4,133
  • 9
  • 30
  • 53
2
votes
2 answers

Avoid duplicates in N Queen iterative solutions (No Recursion Allowed)

I am solving the N queen problem with iteration (no recursion). Problem I am facing right now is duplicate solutions. For example 4 x 4 board has 2 solutions I am printing 4 solutions, so to say I am finding same solution twice. Let me get into the…
Shadid
  • 4,133
  • 9
  • 30
  • 53
2
votes
1 answer

Java: How to implement N-Queens?

I'm studying N-Queens to implement it on my own, and came across the following implementation with the rules: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n,…
Jo Ko
  • 7,225
  • 15
  • 62
  • 120
2
votes
4 answers

N-Queens puzzle solution - cannot understand

I am reading a description to a solution for the N-Queens puzzle on SICP and I cannot understand most of them. Here is the solution: One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed k -…
lightning_missile
  • 2,821
  • 5
  • 30
  • 58
2
votes
1 answer

Query about N- Queen solving?

I solved the N- Queen problem with the condition that there can only be one queen per column. So I place a queen in a square in first column, then move onto the next column and place a queen in a square not attacked by the queen on board. I am able…
Ankit
  • 361
  • 2
  • 13
2
votes
1 answer

Why parallel range processing takes lot more time than Future based parallel processing (N-queens example)?

I came up with two parallel solutions to find as fast as possible one solution for the N queens problem. The first one uses Futures import scala.collection.immutable.HashSet import scala.concurrent.{Await, Future, Promise} import…
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39
2
votes
0 answers

Why is number of solutions of six Queens lesser than that of five Queens?

My reference is to the N-Queens problem, which consists of N queens to be placed on an NxN chess board in such a way that no queen attacks any other queen. This problem is being solved by using backtracking approach. On the Wikipedia page of…
2
votes
1 answer

N-Queens example program strange output

I try the code from the squeen.icl example. When I try it with BoardSize :== 11, there is no problem. But when I change it to 12, the output is [. Why? How to fix that? module squeen import StdEnv BoardSize :== 12 Queens::Int [Int] [[Int]] ->…
sa ma
  • 135
  • 7
2
votes
1 answer

What's wrong with my solution of N-Queens?

The puzzle: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Here's the code: public class Solution { public int totalNQueens(int n) { boolean[][] board = new…
user2916610
  • 765
  • 1
  • 5
  • 12
2
votes
2 answers

Time complexity analysis of backtracking classic n-queens

I have been reading some code of solving the classic n-queens problem. It is just to find one solution(not all of them or to count the # of solutions). You can find complete code on geeksforgeeks website, which is summed up below. The question is,…
Eric Z
  • 14,327
  • 7
  • 45
  • 69