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

4 Queens and 1 Knight to attack all blocks on a 8*8 board

I have a problem at hand which is a variation of the N-Queen-Problem.The problem goes: Find a way to place 4 queens and 1 knight on a 8*8 chessboard, so that all blocks can be attacked by these pieces.It is OK for the pieces to attack each other. I…
antande
  • 169
  • 1
  • 13
2
votes
1 answer

Recursively trying to solve the queen challenge, but getting a weird output

Right now I am trying to solve the problem where there are eight queens on a 8x8 board, and you have to place them where none of them can capture the other. This is pretty popular problem to use recursion on, however I still can't figure out exactly…
FyreeW
  • 395
  • 1
  • 5
  • 18
2
votes
1 answer

Choose list of lists element unique to row and column (N Queens)

I have a list of lists of - characters which acts as a grid. I want to change one - to a Q per col and row. Here's what I've got so far: import pprint import random # I import these classes grid = [['-'] for n in range(8)] for i in…
odisho eivazi
  • 65
  • 1
  • 1
  • 8
2
votes
1 answer

How to Properly convert an Array Output in n-queens

I've been playing around this code for awhile. The main Thing i'm trying to do is experiment with the output. Here is the source of the code: 1 The java code they used is as follows: public class Queens { …
ani ben
  • 145
  • 6
2
votes
1 answer

Solving N-Queens Using Python constraint Resolver

Below is a solution for the N-queens problem using the Python-Constraint Resolver from Labix. Could someone explain to me, or refer me to any web page where it explains the meaning of the last 3 lines of this code? Moreover, how could I use the…
user836026
  • 10,608
  • 15
  • 73
  • 129
2
votes
14 answers

8-Queens algorithm example?

Anybody knows good/concise algorithm examples for 8-queens? I did a Web search and did not find any good example.
northTiger
  • 255
  • 1
  • 2
  • 5
2
votes
2 answers

Here is my swift code for solving eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an…
2
votes
2 answers

Scala solution to nQueen using for-comprehension

I have some difficulty in understanding the Scala solution to the n Queens problem, below is the implementation assuming isSafe is defined correctly def queens(n: Int): Set[List[Int]] = { def placeQueens(k: Int): Set[List[Int]] = k match { …
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
2
votes
1 answer

Prolog - N-Queens quiz - infinite loop

This is about the 8-Queens problem. I'm trying to solve the more generic N-Queens problem. The goal is to make this rule show me all the possible answers. For example: solution(Sol,4). X = [2, 4, 1, 3] ; X = [3, 1, 4, 2] ; false. I managed to get…
Smuzi
  • 23
  • 3
2
votes
1 answer

8 queens puzzle Netbeans(java) graphical interface + SWI prolog

I'm found a solution, only on Bprolog, and ask for a help how to translate it by JPL on SWI PROLOG? OR maybe you can take me solution by jpl libriry using // by Nobukuni Kino import java.awt.*; import java.awt.event.*; import…
2
votes
3 answers

How to execute/skip certain lines of code using preprocessor in C?

I have a simple C program for N-queens calculation. I parallelized it using OpenMP. Now I want to execute both the serial and parallel versions and calculate the speed up. The point is that I don't want to create a new file for the serial code, or…
user1726549
2
votes
1 answer

8 queen solution that use a space state "graph" in Prolog don't work

I am studying Prolog on Ivan Bratko book: Programming for Artificial Intelligence and on the book I have found this version of 8 Queens problem that use a space state "graph" to solve the problem: s(Queens, [Queen|Queens]) :- member(Queen,…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
2
votes
1 answer

From 8-Queens solution to more generic n-Queens solution in Prolog

I am studying Prolog for an universitary exame and I have some problem with the following exercise. I have the following classic solution of 8-Queens problem (and this is not a problem for me), Modifying this solution I have to create a new solution…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
2
votes
1 answer

How would one see if an array has consecutive numbers in C++?

This is basically the 8 Queens problem, but solving it with brute force in a 1D array. Say I have an array (named b) of size 8, with elements ranging from 0 to 7. I initialize each index in the array with 8 for-loops, like this: int b[8]={0}; int…
Ishmael
  • 235
  • 3
  • 8
  • 17
2
votes
2 answers

Variant of N-Queens algorithm

I don't know whether this problem has been studied or not, it just came to my mind while trying out the general N-Queens problem. Given a N*N chessboard , what is the minimum number of Queens required, which, when placed strategically, renders all…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196