4
\$\begingroup\$

I'm looking for ways to optimize a function that I wrote as an answer to this problem in Python:

You and your rescued prisoners need to get out of this collapsing death trap - and fast! Unfortunately, some of the prisoners have been weakened by their long imprisonment and can't run very fast. Their friends are trying to help them, but this escape would go a lot faster if you also pitched in. The exit doors have begun to close, and if you don't make it through in time, you'll be trapped! You need to grab as many prisoners as you can and get through the exit doors before they close.

The time it takes to move from your starting point to all of the prisoners and to the exit door will be given to you in a square matrix of integers. Each row will tell you the time it takes to get to the start, first prisoner, second prisoner, ..., last prisoner, and the exit door in that order. The order of the rows follows the same pattern (start, each prisoner, exit door). The prisoners can jump into your arms, so picking them up is instantaneous, and arriving at the exit door at the same time as it seals still allows for a successful, if dramatic, escape. (Don't worry, any prisoner you don't pick up will be able to escape with you since they no longer have to carry the ones you did pick up.) You can revisit different spots if you wish, and moving to the exit door doesn't mean you have to immediately leave - you can move to and from the exit door to pick up additional prisoners if time permits.

In addition to spending time traveling between prisoners, some paths interact with security checkpoints and add time back to the clock. Adding time to the clock will delay the closing of the exit doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the doors to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is, each time a path is traversed, the same amount of time is used or added.

Write a function of the form answer(times, time_limit) to calculate the most prisoners you can pick up and which prisoners they are, while still escaping through the exit doors before they close for good. If there are multiple sets of prisoners of the same number, return the set of prisoners with the lowest prisoner IDs (as indexes) in sorted order. The prisoners are represented as a sorted list by prisoner ID, with the first prisoner being 0. There are at most 5 prisoners, and time_limit is a non-negative integer that is at most 999.

The function that I wrote is:

def answer(times,time_limit):
    n=len(times)
    mindic=min(min(times))
    dist = map(lambda i : map(lambda j : j , i) , times)
    for k in range(n):
        for i in range(n):
            for j in range(n): 
                dist[i][j] = min(dist[i][j] ,dist[i][k]+ dist[k][j])
    def find_path(times, start, end,time,temp, path=[]):
        if start != 0 and start != end :
            path = path + [start-1]
        if start == end and len(temp) < len(path):
            temp=path
        if start == end and (time-min(times[start][0:-1]) < mindic or len(path)== n-2):
            return path
        best=None
        for i in range(n):
            if time-times[start][i] >= mindic and start != i  and i != 0 and i-1 not in path:
                newpath = find_path(times, i, end,time-times[start][i],temp, path)
                if newpath != None :   
                    if not best or len(newpath) > len(best):
                        best=newpath
        if best == None:
            best=temp
        return best
    res =find_path(dist, 0, n-1,time_limit,[])
    return res

Calling the function will be something like this:

answer([[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]],3)

and the answer for the above example is:

[0, 1]

It will only be able to save the prisoners with index 0 and index 1.

For example, in the case of:

[ 
   [0, 2, 2, 2, -1],  # 0 = Start
   [9, 0, 2, 2, -1],  # 1 = prisoner 0 
   [9, 3, 0, 2, -1],  # 2 = prisoner 1
   [9, 3, 2, 0, -1],  # 3 = prisoner 2
   [9, 3, 2, 2,  0],  # 4 = exit door    
 ]

and a time limit of 1, the five inner array rows designate the starting point, prisoner 0, prisoner 1, prisoner 2, and the exit door respectively. You could take the path:

Start End Delta Time Status

    -   0     -    1 exit door initially open
    0   4    -1    2
    4   2     2    0
    2   4    -1    1
    4   3     2   -1 exit door closes
    3   4    -1    0 exit door reopens; you and the saved persons exit

With this solution, you would pick up prisoner 1 and 2. This is the best combination for this case, so the answer is [1, 2].

Generally, it should be something like this:

Inputs:
    (int) times = [[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]]
    (int) time_limit = 3
Output:
    (int list) [0, 1]

Inputs:
    (int) times = [[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]]
    (int) time_limit = 1
Output:
    (int list) [1, 2]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.