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

for the placequeen function, how does the board.append and board.remove work?

""" Adapted from http://www.prasannatech.net/2012/07/eight-queens-python.html Author: S.Prasanna There are many other solutions to this problem available online. This one is quite easy to understand. Make sure to credit other peoples code when used…
-2
votes
1 answer

N-Queens program in Python

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. I have solved this program earlier, but am trying to rework my code to mirror that which I used to fashion a sudoku solver. I cannot…
Akashvshroff
  • 181
  • 1
  • 9
-2
votes
2 answers

How do you scan a text file and convert it to a 2D char array?

I have been given text files and I am trying to convert them into a 2D char array for further use. What is happening is it appears to be taking the first indices and copying that over and over until it has reached the int size and it believes there…
cb-9099
  • 13
  • 5
-2
votes
1 answer

N queen (algorithm) via array not understanding few lines

http://jeffe.cs.illinois.edu/teaching/algorithms/notes/03-backtracking.pdf enter image description here explain the highlighted texted of image Q[i]=j Q[i]=j+r-i Q[i]=j-r+i how these statements check if two queen attack in row column or diagonals?
Raven
  • 11
  • 4
-2
votes
1 answer

Explanation of 8-Queen solution Java code.

I found a 8-Queen solution that I was able to compile and run in Eclipse. I believe this solution follows the backtracking approach and meat of the work is done in the solution and unsafe functions, but I am having a hard time understanding the code…
Quest Monger
  • 8,252
  • 11
  • 37
  • 43
-2
votes
1 answer

What is the error in my code for the n queen puzzle?

#include #include char a[10][10]; int n; int feasible(int row,int col) { int i,j,tcol; for(i=0;i
gospelslide
  • 179
  • 2
  • 2
  • 12
-2
votes
1 answer

Getting every combination of Queens?

public class SomeQueens { static Stack s= new Stack(); static int Solved = 0; static int current = 0; public static int solve(int n) { // n is 8 while(current < n) { // should I use current < n instead for (int i =…
-2
votes
2 answers

Eight queens print nothing in recursion in C

I am having a C course myself. When I run my code, nothing happened(print nothing), and I can't find out the problem. And I found my check function clumsy, how can i improve it, make it slim and simple? Here's my code. #include #define ON…
ragonci
  • 11
  • 1
-2
votes
1 answer

n-queens Prolog visualizing by special letters

I want to visualize n-queens problem in Prolog. Like this, 1? -queen(1,2,5). =================== # Q # # # # # # Q # Q # # # # # # Q # # # # # # Q =================== =================== # Q # # # # # # # Q # # Q # # Q # # # # # # # Q…
-2
votes
2 answers

Some doubts about 8 queen problems algorithm

I am studying the 8 queen problem and I have think the following algorithm to solve this problem (but it seems not correct) My algorithm work in this way on an 8X8 chessboard: At the beginning put a queen in a random location of the board Mark as…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
-2
votes
1 answer

How to complete this for-loop that calls a function

I am having trouble trying to make a certain for-loop continue to completion in the 1D Queens problem. First, I used goto statements for everything. Now I am trying to get rid of the goto statements by using functions instead. I will eventually get…
Ishmael
  • 235
  • 3
  • 8
  • 17
-3
votes
1 answer

Python List and Dictionary being overwritten or corrupted?

I am trying to brute force solve the N Queen Chess Problem https://en.wikipedia.org/wiki/Eight_queens_puzzle My code is working however unusual behaviour appears when I try and return the solution data structures. The list containing the solutions…
Alex
  • 53
  • 1
  • 2
  • 7
-3
votes
2 answers

eight queens in c using recursion

I'm trying to solve the eight queens problem using recursion, and I am just lost. Can someone please help me?
springday
  • 7
  • 4
-3
votes
2 answers

Algorithm for checking diagonal in N queens algorithm

I am trying to implement N- Queens problem in python. I need a small help in designing the algorithm to check if given a position of Queen check whether any other queen on the board is present on its diagonal or not. I am trying to design a function…
Mihir Thatte
  • 105
  • 1
  • 2
  • 9
-4
votes
1 answer

Function call taking place automatically in n-queen problem

I found this code to find all the possible solutions of an n-queen problem: #include #include int board[20], count; int main() { int n, i, j; void queen(int row, int n); printf(" - N Queens Problem Using Backtracking…
Klasen
  • 159
  • 1
  • 7
1 2 3
23
24