Python 询问“的线索;它';这一切都是关于英里数的;
我最近遇到了一个问题,我真的不知道如何解决它。这是开放式Kattis的一个问题 请访问 现在,我知道这是一个递归问题 但是如何定义这个递归过程呢?我不知道从哪里开始 所以请给我一个线索或者一些伪代码 在这里,我粘贴了用于读取输入的代码,并将输入数据转换为字典Python 询问“的线索;它';这一切都是关于英里数的;,python,Python,我最近遇到了一个问题,我真的不知道如何解决它。这是开放式Kattis的一个问题 请访问 现在,我知道这是一个递归问题 但是如何定义这个递归过程呢?我不知道从哪里开始 所以请给我一个线索或者一些伪代码 在这里,我粘贴了用于读取输入的代码,并将输入数据转换为字典 AFItt = input().split() A, F, I = map(int, AFItt[0:3]) tmin, tmax = map(float, AFItt[3:]) airport = [] ada ={} ai= [] f
AFItt = input().split()
A, F, I = map(int, AFItt[0:3])
tmin, tmax = map(float, AFItt[3:])
airport = []
ada ={}
ai= []
for _ in range(A):
airport.append(input())
for _ in range(F):
ffda = input().split()
if ffda[0] + " " + ffda[1] not in ada.keys():
ada[ffda[0] + " " + ffda[1]] = (float(ffda[2]), float(ffda[3]))
else:
ada[ffda[0] + " " + ffda[1]] += ((float(ffda[2]), float(ffda[3])))
for _ in range(I):
ai.append(input())
我会尽量给你一个线索,但不确定它是否足够有效。我编写了一个javascript版本,它可以正确地生成示例输出 我的解决方案非常简单:从旅程开始,找到所有可能的下一个航班,并继续附加到以前的航班 比如说,
对于前两个行程机场
,我将找到所有可能的航班,并将其保存在一个数组列表中[[fligh1]、[flight2]、[flight3]]
之后,我将循环所有当前可能的运行,并继续检查是否存在可能继续运行的航班。如果不是,则排除在外,如果是,则将航班附加到列表中
如果flight1和flight2无法继续,但flight3有两个可能的航班要继续,我的航班列表将更改为[[flight3,flight4],[flight3,flight5]
我很难解释清楚。以下是一些代码框架:
function findAllFlights(flightMap,
currentFlights,
currentItineraryIndex,
itineraryList, minTime, maxTime){
//flightMap is a map of all the flights. sample data:
/*{'a->b':[{from: 'a', to:'b', depTime:'1', arrTime:'2'}, {another flight}, ... ],
'b->c': [{from: 'b', to:'c', depTime:'1', arrTime:'2'}, {another flight}, ... ]}
*/
//currentFlights is the result of current possible runs, it is a list of list of flights. each sub list means a possible run.
//[[flight1, flight2], [flight1, flight3], ...]
//currentItineraryIndex: this is the next airport index in the itineraryList
//itineraryList: this is the list of airports we should travel.
//minTime, maxTime: it is the min time and max time.
if(currentItineraryIndex == 0){
var from = itineraryList[0];
var to = itineraryList[1];
var flightMapKey = from+'->'+to;
var possibleFlights = flightMap[flightMapKey];
if(possibleFlights.length == 0){
return [];
}
for(var i=0; i<possibleFlights.length; i++){
//current flights should be a list of list of flights.
//each of the sub list denotes the journey currently.
currentFlights.push([possibleFlights[i]]);
}
return findAllFlights(flightMap, currentFlights, 1, itineraryList, minTime, maxTime);
}else if(currentItineraryIndex == itineraryList.length - 1){
//we have searched all the required airports
return currentFlights;
}else{
//this is where you need to recursively call findAllFlights method.
var continableFlights = [];
//TODO: try to produce the continuable list of flights here based on above explanation.
//once we have the continuable flights for current itinerary airport, we can find flights for next airport similarly.
return findAllFlights(flightMap, continableFlights, currentItineraryIndex + 1, itineraryList, minTime, maxTime);
}
}
函数findAllFlights(flightMap,
当前航班,
当前行程索引,
行程列表、minTime、maxTime){
//flightMap是所有航班的地图。示例数据:
/*{'a->b':[{从'a',到:'b',出发时间:'1',到达时间:'2'},{另一个航班},…],
'b->c':[{从'b',到:'c',起飞时间:'1',到达时间:'2'},{另一个航班},…]}
*/
//currentFlights是当前可能运行的结果,它是一个航班列表。每个子列表表示可能的运行。
//[[flight1,flight2],[flight1,flight3],…]
//当前行程索引:这是行程列表中的下一个机场索引
//行程表:这是我们应该去的机场名单。
//minTime,maxTime:它是最小时间和最大时间。
如果(当前行程索引==0){
var from=行程列表[0];
var to=行程列表[1];
var flightMapKey=从+'->'+到;
var-possibleFlights=flightMap[flightMapKey];
if(possibleFlights.length==0){
返回[];
}
对于(var i=0;我感谢您的回复,但我仍然无法完全解决它。主要有两个问题我不知道如何解决。第一个问题是如何以递归方式计算两个相邻航班之间的时间?第二个问题是如何以递归方式追加航班?