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

8 Queens Variation- Number of Arrangements Backtracking Implementation- Wrong Output

I'm trying to write a program that, given an initial input, will count the number of different arrangements of 8 queens on an 8X8 chessboard. For some reason, my test data is not giving me the appropriate output. I was wondering if you guys could…
GiH
  • 365
  • 4
  • 16
3
votes
2 answers

Stackoverflow when calling sol-count on 10 (N-queens program)

So this is my first time programming in clojure and I am running into some issues with stackoverflow with my program. This program basically tries to find all the possible solutions to the N-queens problem…
3
votes
1 answer

8 queens puzzle - recursion using python

I’m trying to solve the 8-queens puzzle, also known as n-queens algorithm. My function should count how many legal ways are there to place N queens on NxN board. I almost got it, but had to do some ugly patch to make it work. Can you help me fix…
Splash
  • 181
  • 1
  • 7
3
votes
1 answer

How to solve n-queens in scheme

I'm trying to solve the n-queens problem in scheme. I was told by my professor to use a single vector as the chess board where the ith element of the vector represents the ith column of the board. The value of that element is the row on which sits…
3
votes
1 answer

n-queens puzzle in java using stacks and backtracking

This is my code for the n-queens problem in java. However, the output is 0 (the number of solutions to 8 queens in this case) when it should be 92. We're supposed to only use stacks and backtracking (no recursion!!). I'm really stuck! any help would…
mm93
  • 31
  • 1
  • 2
2
votes
0 answers

Divergent Efficiency in Concurrent Go Solutions for N-Queens Problem: Seeking Explanation

I'm new to Golang and learning to understand the concurrent programming model. I wrote two concurrent solutions for the N-Queens problem and couldn't find an explanation why the second one is significantly faster than the first(about 25 times…
2
votes
0 answers

Is it possible for backtracking N-queens to produce O(n^2) time complexity

The backtracking approach for n-queens is clearly O(n!). However I am looking at these links here: https://algodaily.com/challenges/classical-n-queen-problem https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/ They give O(n^2) time…
2
votes
3 answers

"Put N Queens", can it possible to run within acceptable time with N = 20?

The task is count how many solutions to put N queens in NxN board. I have tried to thought every possible case to improve the performace, but it take almost 50s to run with N = 15. Here's what I've done: Dim resultCount As Integer = 0 Dim fieldSize…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
2
votes
1 answer

How to get better time complexity of N queens validator

How can we lower the time complexity of this solution of NxN queens matrix validator? I have this solution while I check every row and every column and every diagonal of the matrix. If every row and column has exactly 1 queen, and the matrix has no…
2
votes
3 answers

How to remove mutability from this function in scheme (N-queens)

I'm arduously struggling my way through the N-queens problem in SICP (the book; I spent a few days on it -- last question here: Solving Eight-queens in scheme). Here is what I have for the helper functions: #lang sicp ; the SICP language in Racket…
David542
  • 104,438
  • 178
  • 489
  • 842
2
votes
1 answer

How to identify time and space complexity of recursive backtracking algorithms with step-by-step analysis

Background Information: I solved the N-Queens problem with the C# algorithm below, which returns the total number of solutions given the board of size n x n. It works, but I do not understand why this would be O(n!) time complexity, or if it is a…
2
votes
1 answer

How can I skip duplicate board states in N-Queen problem?

I have written some code which runs fine except the fact that it is also finding duplicate board states. I want to have minimal changes in my code so that it can find only unique board states. I'm pasting my code below: class Solution { …
2
votes
1 answer

How to avoid getting trapped in local minimum in 8-queens using min-conflicts heuristic

I have written the following code to solve the n-queens problem: (defun solve (board max-steps) (enforce-one-queen-per-column board) (dotimes (step max-steps) ; repeat for a max amount of times, (if (eql (get-threatened-queens…
brienna
  • 1,415
  • 1
  • 18
  • 45
2
votes
2 answers

Python N-Queen solution explanation

I was going through this really neat solution for n-queen problem from Elements of Programming Interviews, Recursion chapter, but can't seem to understand a particular piece of code. If someone can explain the logic here, it would be really helpful.…
Mr Matrix
  • 1,128
  • 2
  • 14
  • 28
2
votes
2 answers

N-Queens not printing all solutions

I'm new to recursion and backtracking. I am trying to complete the N-Queen problem to print all solutions instead of just 1 and to understand these concepts. I think I have the algorithm implemented partially right as I get some of the solutions but…
Spindoctor
  • 434
  • 3
  • 17