How would you look at developing an algorithm for this hotel problem?

前端 未结 11 1956
误落风尘
误落风尘 2020-12-13 10:51

There is a problem I am working on for a programming course and I am having trouble developing an algorithm to suit the problem. Here it is:

You are

11条回答
  •  一向
    一向 (楼主)
    2020-12-13 11:37

    It looks like you can solve this problem with dynamic programming. The subproblem is the following:

    d(i) : The minimum penalty possible when travelling from the start to hotel i.

    The recursive formula is as follows:

    d(0) = 0 where 0 is the starting position.

    d(i) = min_{j=0, 1, ... , i-1} ( d(j) + (200-(ai-aj))^2)
    

    The minimum penalty for reaching hotel i is found by trying all stopping places for the previous day, adding today's penalty and taking the minimum of those.

    In order to find the path, we store in a separate array (path[]) which hotel we had to travel from in order to achieve the minimum penalty for that particular hotel. By traversing the array backwards (from path[n]) we obtain the path.

    The runtime is O(n^2).

提交回复
热议问题