Dynamic tree construction with recursive return

I have this problem that I need to solve the recursive backtracking problem. It is very similar to the n-queen problem, but differs in that it uses different candidates with an a-symmetrical board. In total, there are four different candidates, each of which depends on both. I have 2 aces, 2 kings, 2 queens and 2 jacks. Each ace should be next to (horizontal or vertical) with the king, each king should be next to the queen, and each queen should be next to the jack, and not the pieces can have duplicates next to them. The board with the correct solution looks like this:

Grid (y, x)(only the positions between *y,x* are available for candidates): 
4,1 4,2 *4,3* 4,4
3,1 *3,2* *3,3* *3,4*
*2,1* *2,2* *2,3* 2,4
1,1 1,2 *1,3* 1,4

Possible Solution
. . K . 
Q J Q .
. A K A
. . J .

Now my problem is that I want to use the tree to track the candidates as parents and children of the tree. I haven't implemented a tree yet, but I was wondering if the method shown in this example is a good way to create a tree. And if this is a good way to create a tree, how do I start, how does the tree know which parent element the child should have, and also return when the solution does not fit?

Hope I have added enough information about this situation, thanks in advance.

+3
source share
1 answer

, , , , . , , , . http://en.wikipedia.org/wiki/Backtracking, , , (, reject true) .

, , , , .

+1

Source: https://habr.com/ru/post/1791498/


All Articles