如何获得郊区对象之间的累计距离时间?

本文关键字:距离 时间 何获得 郊区 对象 之间 | 更新日期: 2023-09-27 18:05:53

这是我对这个问题的最后一次代码编辑。这是我能想到的最好的解决方案。如果有人有其他的解决方案,我仍然愿意接受。

我正在寻找关于在线性郊区链中查找两个非连续郊区之间的累积时间或距离的最佳方法的建议,如果我有每个郊区之间的时间/距离。我将原来的问题表达为:

我知道从郊区A到郊区B开车需要的时间,郊区B和郊区C,郊区C和郊区D等等。我怎样才能算出两者之间的车程非连续郊区(例如郊区C和郊区G)。

由于它被归类为过于宽泛,我一直在研究它并提出了这个解决方案。我想知道是否有一种更有效的或面向未来的解决方案。

给定一个定义如下的郊区类:

public class Suburb
{
    public string name;
    public int index;
    public double timeToNextSuburb;
    public double kilometresToNextSuburb;
    public double timeToPreviousSuburb;
    public double kilometresToPreviousSuburb;
    public Suburb(int index, string name, double time, double kilometres)
    {
        this.name = name;
        this.index = index;
        this.timeToNextSuburb = time;
        this.kilometresToNextSuburb = kilometres;
    }
}

和定义如下的郊区对象列表:

 public static class SuburbManager
    {
                public static List<Suburb> InitializeSuburbs()
    {
        var baseList = new List<Tuple<string, int, double>>{
                new Tuple<string,int,double>("Start",0,0),
                new Tuple<string,int,double>("a",6,6.4),
                new Tuple<string,int,double>("b",5,4.8),
                new Tuple<string,int,double>("c",8,6.3),
                new Tuple<string,int,double>("d",5,2.4),
                new Tuple<string,int,double>("e",6,4.7),
                new Tuple<string,int,double>("f",7,6.8),
                new Tuple<string,int,double>("g",3,2.5),
                new Tuple<string,int,double>("h",3,3.2),
                new Tuple<string,int,double>("i",3,2.8),
                new Tuple<string,int,double>("j",7,6.3),
                new Tuple<string,int,double>("k",4,4.0),
                new Tuple<string,int,double>("l",3,3.4),
                new Tuple<string,int,double>("m",3,3.3),
                new Tuple<string,int,double>("n",4,3.0),
                new Tuple<string,int,double>("o",0,0),
                new Tuple<string,int,double>("p",0,0)
            };
        var suburbList = baseList.Select((t, i) => new Suburb(i - 1, t.Item1, t.Item2, t.Item3)).ToList();
        for(var i = 1; i < suburbList.Count - 1; i++)
        {
            suburbList[i].timeToPreviousSuburb = suburbList[i - 1].timeToNextSuburb;
            suburbList[i].kilometresToPreviousSuburb = suburbList[i - 1].kilometresToPreviousSuburb;
        }
        return suburbList;
    }

和以下方法:

        public static double GetSuburbTime(string suburbName1, string suburbName2)
    {
        int suburb1ID, suburb2ID;
        if ((suburb1ID = GetSuburbID(suburbName1)) < (suburb2ID = GetSuburbID(suburbName2)))
        {
            return InitializeSuburbs().Where(suburbs => suburbs.index >= suburb1ID && suburbs.index < suburb2ID).Sum(suburb => suburb.timeToNextSuburb);
        }
        return InitializeSuburbs().Where(suburbs => suburbs.index >= suburb2ID && suburbs.index < suburb1ID).Sum(suburb => suburb.timeToNextSuburb);
    }
public static int GetSuburbID(string suburbName)
    {
        return InitializeSuburbs().First(suburb => suburb.name == suburbName).index;
    }
    public static string GetCurrentSuburb(string suburbName1, string suburbName2, double minutesElapsed)
    {
        int suburb1ID, suburb2ID;
        List<Suburb> suburbRange;
        if((suburb1ID = GetSuburbID(suburbName1)) < (suburb2ID = GetSuburbID(suburbName2)))
        {
            suburbRange = InitializeSuburbs().Where(suburbs => suburbs.index >= suburb1ID && suburbs.index < suburb2ID).OrderBy(suburbs => suburbs.index).ToList();
            for (var i = 0; i < suburbRange.Count(); i++)
            {
                if (minutesElapsed < suburbRange.GetRange(0, i + 1).Sum(suburb => suburb.timeToNextSuburb))
                {
                    return suburbRange[i].name;
                }
            }
        }
        else
        {
            suburbRange = InitializeSuburbs().Where(suburbs => suburbs.index >= suburb2ID && suburbs.index < suburb1ID).OrderByDescending(suburbs => suburbs.index).ToList();
            for(var i = 0; i < suburbRange.Count(); i++)
            {
                if(minutesElapsed < suburbRange.GetRange(0, i + 1).Sum(suburb => suburb.timeToPreviousSuburb))
                {
                    return suburbRange[i].name;
                }
            }
        }
        return "";
    }

很抱歉有这么多代码块,但是GetTime方法本质上是获取包含所需郊区的子数组,并计算它们之间的时间,getcurrentsuburban方法检查开车时经过了多少分钟,并且给定您开始和结束时所在的郊区,给出您当前所在的郊区。

由于这是如此多的代码为一个小问题,如果有一个更简单的方法做到这一点,我将感谢任何反馈。

谢谢。

如何获得郊区对象之间的累计距离时间?

你可以使用图来解决这个问题,你可以使用Dijkstra算法计算两个郊区之间的最短路径,也可以使用Floyd-Warshall算法预先计算任何一对郊区的所有成本。