列出给定模型铁路上可能的火车路线 [英] List possible train routes on a given model railway

查看:86
本文介绍了列出给定模型铁路上可能的火车路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算给定模型铁路上的可用路线。

I want to calculate the available routes on a given model railway.

假设:


  • 所有火车仅在称为火车站的已定义点上开始和停止。

  • 火车在旅行过程中不会发生碰撞。不必担心。

  • 我们不必考虑火车的长度。我们假设它是一个占据原子空间的机车。

  • 在一个火车站上,只有一列火车可以停下来。

  • 火车可以启动在同一火车站停下。

  • All trains only start and stop on defined points which are called train stations.
  • Trains do not collide during their trip. No need to worry about that.
  • We don't have to think about the length of a train. We assume it is one locomotive which takes the space of an atom.
  • On one train station only one train can stop.
  • A train can start and stop on the same train station.

所有火车的开始位置和结束位置都存储在地图中。所有排列都存储在列表中。示例:

The start position and the end position of all trains are stored in a map. All permutations are stored in a list. Example:

iex(1)> Trains.list_routes(["ICE"], ["Hamburg", "Frankfurt"])
[
  %{end: %{"ICE" => "Hamburg"}, start: %{"ICE" => "Hamburg"}},
  %{end: %{"ICE" => "Frankfurt"}, start: %{"ICE" => "Frankfurt"}},
  %{end: %{"ICE" => "Frankfurt"}, start: %{"ICE" => "Hamburg"}},
  %{end: %{"ICE" => "Hamburg"}, start: %{"ICE" => "Frankfurt"}}
]

铁路模型可能看起来像这样(红色数字表示火车站):

A model railway could look like this (the red numbers indicate the train stations):

对于该模型铁路上的两列火车,其功能为这样称呼:

For two trains on that model railway the function would be called this way:

Trains.list_routes([:red_train, :blue_train], ["1", "2", "3", "4", "5"])

这是我当前的代码:

defmodule Trains do
  @moduledoc """
  Documentation for `Trains`.
  """

  @doc """
  Returns a list of all possible routes.

  ## Examples

      iex> Trains.list_routes([:red_train, :blue_train], ["Station 1", "Station 2"])
      [
        %{
          end: %{blue_train: "Station 2", red_train: "Station 1"},
          start: %{blue_train: "Station 2", red_train: "Station 1"}
        },
        %{
          end: %{blue_train: "Station 1", red_train: "Station 2"},
          start: %{blue_train: "Station 1", red_train: "Station 2"}
        },
        %{
          end: %{blue_train: "Station 1", red_train: "Station 2"},
          start: %{blue_train: "Station 2", red_train: "Station 1"}
        },
        %{
          end: %{blue_train: "Station 2", red_train: "Station 1"},
          start: %{blue_train: "Station 1", red_train: "Station 2"}
        }
      ]
  """
  def list_routes([], []) do
    []
  end

  def list_routes([train], [station]) do
    [
      %{start: %{train => station}, end: %{train => station}}
    ]
  end

  def list_routes([train], [station1, station2]) do
    [
      %{start: %{train => station1}, end: %{train => station1}},
      %{start: %{train => station2}, end: %{train => station2}},
      %{start: %{train => station1}, end: %{train => station2}},
      %{start: %{train => station2}, end: %{train => station1}}
    ]
  end

  def list_routes([train1, train2], [station1, station2]) do
    [
      %{
        start: %{train1 => station1, train2 => station2},
        end: %{train1 => station1, train2 => station2}
      },
      %{
        start: %{train1 => station2, train2 => station1},
        end: %{train1 => station2, train2 => station1}
      },
      %{
        start: %{train1 => station1, train2 => station2},
        end: %{train1 => station2, train2 => station1}
      },
      %{
        start: %{train1 => station2, train2 => station1},
        end: %{train1 => station1, train2 => station2}
      }
    ]
  end

  def list_routes(trains, train_stations) do
    # ???
  end
end

如何使用 list_routes遍历所有组合(火车,火车站),当火车的数量和 train_stations 的数量更大时大于1?

How can I loop through all combinations with list_routes(trains, train_stations) when the number of trains and the number of train_stations is bigger than 1?

推荐答案

以下是该问题的解决方案。它使用公式

Here's a solution for the problem. It uses Formulae.

混合。 exs

def deps do
  [{:formulae, "~> 0.8"}]
end

lib / trains.ex

def list_routes([], []) do
  []
end

def list_routes(trains, train_stations)
    when is_list(trains) and
            is_list(train_stations) and 
            length(train_stations) >= length(trains) do
  possible_states =
    Enum.map(Formulae.permutations(train_stations, length(trains)), &Enum.zip(trains, &1))

  for state_start <- possible_states, state_end <- possible_states do
    %{start: state_start, end: state_end}
  end
end

结果:

iex(1)> Trains.list_routes([:red_train, :blue_train], ["Station 1", "Station 2"])
[
  %{
    end: [red_train: "Station 1", blue_train: "Station 2"],
    start: [red_train: "Station 1", blue_train: "Station 2"]
  },
  %{
    end: [red_train: "Station 2", blue_train: "Station 1"],
    start: [red_train: "Station 1", blue_train: "Station 2"]
  },
  %{
    end: [red_train: "Station 1", blue_train: "Station 2"],
    start: [red_train: "Station 2", blue_train: "Station 1"]
  },
  %{
    end: [red_train: "Station 2", blue_train: "Station 1"],
    start: [red_train: "Station 2", blue_train: "Station 1"]
  }
]

这篇关于列出给定模型铁路上可能的火车路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆