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

8 Non-Attacking Queens Algorithm with Recursion

I'm having trouble coding the 8 queens problem. I've coded a class to help me solve it, but for some reason, I'm doing something wrong. I kind of understand what's supposed to happen. Also, we have to use recursion to solve it but I have no clue how…
Tlazolteotl
  • 41
  • 1
  • 5
4
votes
1 answer

N Queens in C++ using vectors

I'm having trouble understanding backtracking, I can conceptually understand that we make a move, then if no solutions can be found out of it we try the next solution. With this in mind I'm trying to solve the N Queens problem, I'm finding out all…
nikhil
  • 8,925
  • 21
  • 62
  • 102
3
votes
1 answer

N-Queens Python solution generating incorrect output

I am trying to solve the n-queens problem. I wrote a solution for the problem as follows: def isSafe(board,row,col): for i in range(row): if board[i][col]==1: return False x,y = row,col # print(x,y) while…
3
votes
1 answer

How to optimize a n-queens OpenMP parallel program?

I'm working on parallelizing the n-queens problem using OpenMP, but my sequential program is just as fast. I've been trying to use num_threads, but I don't think I am doing it correctly. Can someone look at my code and tell me what I am doing wrong…
3
votes
1 answer

Why do the Even Ns take longer than the Odd Ns?

I have some code here that solves the n queens problem using backtracking in python. When I run it, the odds always take much less time than the evens. This is especially evident when the n gets to around 20+. Does anyone know why this is? import…
The Guy
  • 33
  • 2
3
votes
1 answer

Optimizing n-queens in Haskell

This code: {-# LANGUAGE BangPatterns #-} module Main where import Data.Bits import Data.Word import Control.Monad import System.CPUTime import Data.List -- The Damenproblem. -- Wiki: https://de.wikipedia.org/wiki/Damenproblem main :: IO () main…
devsteff
  • 108
  • 1
  • 7
3
votes
4 answers

8 queens problem

How can i go about implemting 8/4 queens problem?Should i use DFS/BFS,I think DFs will be better. Can any one give some pseudocode/guidlines?
akshay
  • 365
  • 2
  • 5
  • 9
3
votes
1 answer

How does this n-queens Prolog solution work?

I am trying to understand how this n-queens Prolog solution from N-Queens Problem‍​..How far can we go? works (there were no comments on the original page). I have tried to add comments where I could understand something: generate([],_). %…
rnso
  • 23,686
  • 25
  • 112
  • 234
3
votes
2 answers

n-queens solution not working in Prolog

I am trying to run following code from N-Queens Problem‍​..How far can we go? to find solutions to n-queens problem: generate([],_). generate([H|T],N) :- H in 1..N , generate(T,N). lenlist(L,N) :- lenlist(L,0,N). lenlist([],N,N). lenlist([_|T],P,N)…
rnso
  • 23,686
  • 25
  • 112
  • 234
3
votes
1 answer

How to convert an ArrayList> to List> in Java?

I'm working on the N-Queens problem on LeetCode, which stipulates that the return type for the main method is a List>. I thought that if I made a global variable that was a List>, then in main instantiated it as an…
vball
  • 359
  • 1
  • 14
3
votes
2 answers

Solving Eight Queens problem with 2-d array: IndexOutOfBounds error

One of my homework assignments is to solve the Eight Queens problem using a two dimensional array to represent the board. I keep getting an index out of bounds error:8 in my "isUnderAttack" method at: if (board[row][j] == QUEEN) and in my…
Bell
  • 69
  • 4
  • 12
3
votes
2 answers

maximum number of non attacking pairs of queens in 8 queens

Maximum number of non-attacking pairs of queens in 8-Queens problem is given by 8 × 7/2 = 28. Can someone explain how it is 8x7/2?
user5492770
  • 357
  • 3
  • 5
  • 13
3
votes
1 answer

More efficient algorithm to count attacks in N-queens?

I'm working on a DFS based solution to the N-queens problem. I store the board state as an int[N] array representing the vertical placements of queens in each column (eg. placement of the queens diagonally down a 6x6 board would be state = { 0, 1,…
Brendan Hill
  • 3,406
  • 4
  • 32
  • 61
3
votes
1 answer

Implementing Backtracking N-Queens Algorithm

I am implementing an algorithm in C to solve the N-Queens problem. My code solves the problem for n = 4, but doesn't work for any other values of n. I think the issue may be in the print code, but I am unsure. I've tried changing the conditions in…
3
votes
2 answers

Resolve 16-Queens Problem in 1 second only

I should resolve 16-Queens Problem in 1 second. I used backtracking algorithm like below. This code is enough to resolve N-Queens Problem in 1 second when the N is smaller than 13. But it takes long time if N is bigger than 13. How can I improve…
Raymond
  • 473
  • 3
  • 18
1 2
3
23 24