Advertisement
Guest User

Untitled

a guest
Jul 14th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.39 KB | None | 0 0
  1. # количество пар стрелок
  2. N = 1
  3.  
  4. # каждая стрелка имеет 1 вход и 2 выхода
  5. @enum Way I O
  6.  
  7. struct SwitchEnd
  8.     switch::Int64
  9.     way::Way
  10. end
  11.  
  12. # depth - текущая глубина рекурсии
  13. # total_switches - всего стрелок
  14. # untouched_switch - текущая неподключенная стрелка
  15. # free_ends - не подключенные пути
  16. function allcons(depth::Int64, total_switches::Int64, untouched_switch::Int64, free_ends::AbstractArray{SwitchEnd,1})::Array{Array{SwitchEnd,1},1}
  17.     result = Array{SwitchEnd,1}[]
  18.     local_free_ends = free_ends
  19.     local_untouched_switch = untouched_switch
  20.     #println("depth=", depth, " free_ends=", free_ends, " untouched_switches=", untouched_switches)
  21.     # В самом начале берём первую свободную стрелку и перечисляем её пути.
  22.     # Если у нас закончились свободные пути в середине процесса,
  23.     # значит образовались не связанные стрелки - не ведём рекурсию дальше.
  24.     if size(free_ends, 1) == 0 && untouched_switch <= total_switches && depth == 0
  25.         ns = local_untouched_switch
  26.         local_untouched_switch = local_untouched_switch + 1
  27.         local_free_ends = [SwitchEnd(ns, I), SwitchEnd(ns, O), SwitchEnd(ns, O)]
  28.         #println("Fill ends", local_free_ends)
  29.     end
  30.     if size(local_free_ends, 1) > 0
  31.         # всегда берём первый свободный путь
  32.         pair1 = local_free_ends[1]
  33.         local_free_ends = view(local_free_ends, 2 : size(local_free_ends, 1))
  34.        
  35.         # При переборе остальных свободных путей
  36.         # пропускаем уже проверенный, чтобы дважды не проверять выходы одной стрелки
  37.         prev_pair2 = nothing
  38.         for i = 1 : size(local_free_ends, 1)
  39.             pair2 = local_free_ends[1]
  40.             if pair2 !== prev_pair2
  41.                 local_free_ends1 = view(local_free_ends, setdiff(1 : size(local_free_ends, 1), i))
  42.                 if size(local_free_ends1, 1) > 0
  43.                     for l = allcons(depth + 1, total_switches, local_untouched_switch, local_free_ends1)
  44.                         push!(result, [[pair1, pair2]; l])
  45.                     end
  46.                 elseif local_untouched_switch > total_switches
  47.                     # добавляем результат только если дошли до конца
  48.                     push!(result, [pair1, pair2])
  49.                 end
  50.                 prev_pair2 = pair2
  51.             end
  52.         end
  53.         if local_untouched_switch <= total_switches
  54.             # берём первую необработанную стрелку
  55.             ns = local_untouched_switch
  56.             local_untouched_switch = local_untouched_switch + 1
  57.             new_local_free_ends = [ SwitchEnd(ns, I), SwitchEnd(ns, O), SwitchEnd(ns, O) ]
  58.             # проверяем только один вход и один выход
  59.             for j = 1 : 2
  60.                 pair2 = new_local_free_ends[j]
  61.                 for l = allcons(depth + 1, total_switches, local_untouched_switch, [local_free_ends; view(new_local_free_ends, setdiff(1 : 3, j))])
  62.                     push!(result, [[pair1, pair2]; l])
  63.                 end
  64.             end
  65.         end
  66.     else
  67.         # nothing found
  68.         push!(result, SwitchEnd[])
  69.     end
  70.     result
  71. end
  72.  
  73. res = allcons(0, 2 * N, 1, SwitchEnd[])
  74.  
  75. println(size(res, 1))
  76.  
  77. open("t.txt", "w") do f
  78.     for l = res
  79.         for i = l
  80.             write(f, string(i.switch))
  81.             write(f, string(i.way))
  82.         end
  83.         write(f, "\n")
  84.     end
  85. end
  86.  
  87. #display(res)
  88.  
  89. println(size(res, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement