D
D
Dmitry Stoyanov2016-04-14 21:38:19
Java
Dmitry Stoyanov, 2016-04-14 21:38:19

Java. N queens, on the board, need an algorithm to check, "what if the queen can stand in the same place" or how to find all the ways?

Problem: Given an NxN chessboard. It is necessary to place N queens on it so that none threatens the other. The program should display all possible options for such an arrangement.
The problem is actually. That everything is ready, it remains to think out the method of placement, because. my algorithm does not take into account the fact that the queen can stand in the same place, while other queens will be located differently, i.e. need to make a "correction for repetition".
Another example: the program fulfills the conditions only for a board no larger than 4x4. If the board is 5x5, then it gives out 4 ways as a result, although in fact there should be 10 of them.
https://drive.google.com/folderview?id=0B3gMuZG1Lo...
I would be glad to see the implementation of the algorithm I need.
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Stoyanov, 2016-04-28
@stoyanovdmitry

www.java.achchuthan.org/2012/02/n-queens-problem-i...

R
Rsa97, 2016-04-14
@Rsa97

The most obvious algorithm:
It is obvious that there cannot be two queens in the same column or row. Thus, all options can be represented as permutations of N numbers from 1 to N, where the position of the number is the number of the vertical, and the number itself is the number of the horizontal. It remains to generate such permutations and check their diagonals if |Pi-Pj| = |ij|, then the queens are on the same diagonal and the position is invalid.
Well, then you can optimize the algorithm by checking the diagonals at the stage of generating permutations.

F
fish72, 2019-12-05
@fish72

the program "QueensProblem.exe" arranges queens on a square board:
https://yadi.sk/d/ZSKQmtIFtVhatw

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question