这是我对这个问题的最终代码编辑。这是我能想到的最好的解决方案。如果有人有这些解决方案,我仍然愿意接受其他解决方案。
如果我有时间/距离的距离,我正在寻找关于找到两个非连续郊区之间的累积时间或距离的最佳方法的建议,在郊区的线性链中每一个之间。我把原来的问题表达为:
我知道在郊区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方法基本上获取了包含所需郊区的子数组,并计算它们之间的时间,GetCurrentSuburb方法检查驱动时已经过了多少分钟,并且给定你开始的郊区和你将完成的郊区,为你提供你目前所在的郊区。
由于这是一个针对小问题的大量代码,如果有更简单的方法,我会很感激任何反馈。
谢谢。