Answer the question
In order to leave comments, you need to log in
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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question