All possible permutations and related data OR other solutions

leorob88

Well-known member
Joined
Dec 31, 2020
Messages
59
Programming Experience
1-3
I would make a program to calculate a sequence, taken from a game i played.
Basically you have a sequence of numbers, it is like a clock really, meaning they're disposed in a circle. These numbers are 1 digits only, from 1 to a max of 6, but there can be duplicates of numbers along the circle, meaning the sequence can be for example 2, 2, 3, 2, 2 (starting from top/north position and following the circumference clockwise).
The purpose of this kind of game is "selecting" ALL the numbers in the sequence but selecting them all just ONCE. You can choose where to start from. When you select a number, you can choose next only the two other numbers distancing by the number you selected. By two numbers I mean you can choose distancing forward or backwards. For examples, you select a 3, you can next select only the two numbers which have a distance of 3 from the 3. So, in the example case, the selectable values would both be 2, but in different positions. If per chance one of the two positions is unselectable, since you already selected it earlier, you cannot select it again, you can only select the others. If both are unavailable, you lose (as you indeed failed to create a sequence of selecting all numbers not selecting the same multiple times).
In the example case that I could sample like this:
2
2
3
2
2
the solution could be this (there is more than one solution in this example but this is one possible solution)
2 1st select
2 4th select
3 2nd select
2 5th select
2 3rd select
As you see, after the 3 you move backwards and so you choose the last in the list as 3rd selection. Then you move forward again restarting from the top of the list.
So, my goal would be to create a program which calculates a possible solution for this game, given a starting sequence of values. Only, the list of values can be up to 13 values. I thought also about calculating permutations but it would be 13 factorial... Once the solution is found, i need to write (messagebox, textbox, label, whatever you want) the sequence of the numbers selected and the positions in the original list (as there can be duplicate values so I need to know exactly what position that number is). How would you advice to handle this thing?
 
Last edited:
What's wrong with just a basic depth first tree search?
 
See:



lol at first you made it sound like it should be obvious! So, I tried reading. The general concept on wikipedia i kinda get. Only I don't understand properly the code and how to apply it, even more because my case is some sort of ring/circle structure and not a tree graph, so I don't really understand how to apply this difference too. In the second one there is not even a line of code so I found it more of a conceptual explanation. The third link actually shows more comprehensible code, kinda, and the recursive code seems more useful than the iterative, only i don't know Python and

Python:
def dfs_recursive(graph, node, visited):
    if node not in visited:
        print(node)
        visited.add(node)
        for neighbor in graph[node]:
            dfs_recursive(graph, neighbor, visited)

# Example usage
graph = {
    'A': ['B', 'C'],
    'B': ['D', 'E'],
    'C': ['F'],
    'D': [],
    'E': ['F'],
    'F': []
}

visited = set()
dfs_recursive(graph, 'A', visited)

Is set() supposed to be like an Array or List? Also, in the case of this code example each node in graph has assigned related data (like, A -> B, C). For the purpose of my program, I suppose these 2 should be the numbers you can select next, when you select a number? I have this doubt just because I see in this example D and F have no related nodes.
 
Back
Top Bottom