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
1 answer

My code is showing compiler erros and I dont know why even though the code seems correct

Ok so I finished my code n queens genetics in c# but I keep getting these compiler errors even though I changed the code several times using System; using System.Collections.Generic; using System.Linq; using System.Text; using…
-4
votes
1 answer

n queens program in python doesn't work

I have a problem with my program in python of n queens (how many possible ways are to place n queens in a nxn board). Seems like there's a problem with my recursion but I'm really helpless. Could someone figure out what's wrong? def queens(N): …
CnR
  • 351
  • 1
  • 6
  • 15
-5
votes
1 answer

Kattis Eight Queens exercise

Attempting the Eight Queens problem in Python (https://open.kattis.com/problems/8queens). I've written some code which works for all input I've been able to imagine for the last hour - but the program still fails the Kattis test cases. It's not very…
-10
votes
1 answer

Eight queens in java

Here is my code for the eight queens problem. I checked it with many test case and it's correct. But when I submitted it to https://open.kattis.com/, it noticed me that my code is the wrong answer. So, where is my code fails? Please help! public…
trueterry
  • 1
  • 3
1 2 3
23
24