Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # количество пар стрелок
- N = 1
- # каждая стрелка имеет 1 вход и 2 выхода
- @enum Way I O
- struct SwitchEnd
- switch::Int64
- way::Way
- end
- # depth - текущая глубина рекурсии
- # total_switches - всего стрелок
- # untouched_switch - текущая неподключенная стрелка
- # free_ends - не подключенные пути
- function allcons(depth::Int64, total_switches::Int64, untouched_switch::Int64, free_ends::AbstractArray{SwitchEnd,1})::Array{Array{SwitchEnd,1},1}
- result = Array{SwitchEnd,1}[]
- local_free_ends = free_ends
- local_untouched_switch = untouched_switch
- #println("depth=", depth, " free_ends=", free_ends, " untouched_switches=", untouched_switches)
- # В самом начале берём первую свободную стрелку и перечисляем её пути.
- # Если у нас закончились свободные пути в середине процесса,
- # значит образовались не связанные стрелки - не ведём рекурсию дальше.
- if size(free_ends, 1) == 0 && untouched_switch <= total_switches && depth == 0
- ns = local_untouched_switch
- local_untouched_switch = local_untouched_switch + 1
- local_free_ends = [SwitchEnd(ns, I), SwitchEnd(ns, O), SwitchEnd(ns, O)]
- #println("Fill ends", local_free_ends)
- end
- if size(local_free_ends, 1) > 0
- # всегда берём первый свободный путь
- pair1 = local_free_ends[1]
- local_free_ends = view(local_free_ends, 2 : size(local_free_ends, 1))
- # При переборе остальных свободных путей
- # пропускаем уже проверенный, чтобы дважды не проверять выходы одной стрелки
- prev_pair2 = nothing
- for i = 1 : size(local_free_ends, 1)
- pair2 = local_free_ends[1]
- if pair2 !== prev_pair2
- local_free_ends1 = view(local_free_ends, setdiff(1 : size(local_free_ends, 1), i))
- if size(local_free_ends1, 1) > 0
- for l = allcons(depth + 1, total_switches, local_untouched_switch, local_free_ends1)
- push!(result, [[pair1, pair2]; l])
- end
- elseif local_untouched_switch > total_switches
- # добавляем результат только если дошли до конца
- push!(result, [pair1, pair2])
- end
- prev_pair2 = pair2
- end
- end
- if local_untouched_switch <= total_switches
- # берём первую необработанную стрелку
- ns = local_untouched_switch
- local_untouched_switch = local_untouched_switch + 1
- new_local_free_ends = [ SwitchEnd(ns, I), SwitchEnd(ns, O), SwitchEnd(ns, O) ]
- # проверяем только один вход и один выход
- for j = 1 : 2
- pair2 = new_local_free_ends[j]
- for l = allcons(depth + 1, total_switches, local_untouched_switch, [local_free_ends; view(new_local_free_ends, setdiff(1 : 3, j))])
- push!(result, [[pair1, pair2]; l])
- end
- end
- end
- else
- # nothing found
- push!(result, SwitchEnd[])
- end
- result
- end
- res = allcons(0, 2 * N, 1, SwitchEnd[])
- println(size(res, 1))
- open("t.txt", "w") do f
- for l = res
- for i = l
- write(f, string(i.switch))
- write(f, string(i.way))
- end
- write(f, "\n")
- end
- end
- #display(res)
- println(size(res, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement