fork download
  1. def find_10_10_tours():
  2. n = 16
  3. solutions = []
  4.  
  5. def backtrack(seq):
  6. # If we've built a sequence of length 10:
  7. if len(seq) == n:
  8. # Check if it ends with 9:
  9. if seq[-1] == 15:
  10. solutions.append(seq[:]) # Store a copy of this valid tour
  11. return
  12.  
  13. used = set(seq)
  14.  
  15. # Try all possible next values from 1..10
  16. for next_val in range(1, n+1):
  17. if next_val not in used:
  18. # We only allow next_val if it can be expressed
  19. # as a_i - a_j for some i,j < current length,
  20. # with a_i > a_j.
  21. valid = False
  22. # For l < 3, we can skip the difference check if l=2?
  23. # Actually, the problem states "for l >= 3," so for the second position
  24. # (index 1 in Python), there is no difference requirement yet.
  25. # We'll handle that condition carefully:
  26. if len(seq) < 2:
  27. # If we haven't placed the second element yet,
  28. # there's no difference requirement. We can pick anything not used.
  29. valid = True
  30. else:
  31. # Now we must see if next_val is a difference of two earlier terms
  32. for i in range(len(seq)):
  33. for j in range(len(seq)):
  34. if i != j and seq[i] > seq[j]:
  35. if seq[i] - seq[j] == next_val:
  36. valid = True
  37. break
  38. if valid:
  39. break
  40.  
  41. if valid:
  42. seq.append(next_val)
  43. backtrack(seq)
  44. seq.pop()
  45.  
  46. # We start the sequence with a1 = 10
  47. initial_seq = [16]
  48. backtrack(initial_seq)
  49.  
  50. return solutions
  51.  
  52.  
  53. # --- Run the search and display results ---
  54. all_tours = find_10_10_tours()
  55.  
  56. print("All (10,10)-tours found (each sequence of length 10):")
  57. for tour in all_tours:
  58. print(tour)
  59.  
  60. print(f"\nTotal number of (10,10)-tours found: {len(all_tours)}")
Success #stdin #stdout 0.02s 25860KB
stdin
Standard input is empty
stdout
def find_10_10_tours():
    n = 16
    solutions = []

    def backtrack(seq):
        # If we've built a sequence of length 10:
        if len(seq) == n:
            # Check if it ends with 9:
            if seq[-1] == 15:
                solutions.append(seq[:])  # Store a copy of this valid tour
            return

        used = set(seq)
        
        # Try all possible next values from 1..10
        for next_val in range(1, n+1):
            if next_val not in used:
                # We only allow next_val if it can be expressed
                # as a_i - a_j for some i,j < current length,
                # with a_i > a_j.
                valid = False
                # For l < 3, we can skip the difference check if l=2?
                # Actually, the problem states "for l >= 3," so for the second position
                # (index 1 in Python), there is no difference requirement yet.
                # We'll handle that condition carefully:
                if len(seq) < 2:
                    # If we haven't placed the second element yet,
                    # there's no difference requirement. We can pick anything not used.
                    valid = True
                else:
                    # Now we must see if next_val is a difference of two earlier terms
                    for i in range(len(seq)):
                        for j in range(len(seq)):
                            if i != j and seq[i] > seq[j]:
                                if seq[i] - seq[j] == next_val:
                                    valid = True
                                    break
                        if valid:
                            break

                if valid:
                    seq.append(next_val)
                    backtrack(seq)
                    seq.pop()

    # We start the sequence with a1 = 10
    initial_seq = [16]
    backtrack(initial_seq)

    return solutions


# --- Run the search and display results ---
all_tours = find_10_10_tours()

print("All (10,10)-tours found (each sequence of length 10):")
for tour in all_tours:
    print(tour)

print(f"\nTotal number of (10,10)-tours found: {len(all_tours)}")