简体   繁体   中英

Dynamic programming - catching ball

I'm trying to solve the following problem:

Balls are falling from the sky. We know at which location (on a straight line) will each ball drop, and we know the time (in seconds) at which will the ball reach the ground. We are trying to catch them into a ball net, which we can move left or right, but each movement costs 1 second. The initial position of a ballnet is always on the left (position 0). We are allowed to > drop (not catch) k number of balls.

What is the highest score we can achieve?

My first attempt at solving this was a greedy algorithm:

if the |next ball position - current position of the ball net| > (time of the next ball - current time) 
     then attempts++
     if attempts>k
           print game over
else
     current ball net position = next ball position
     current time = time of the next ball
     score++

however my algorithm doesn't take into consideration that sometimes it's better to sacrifice some number of balls in order to reach a higher score in the long run. This needs an approach via dynamic programming, I think.

Is this problem a known one so I can find some help? Could you help me with this problem? I can solve this in a greedy way, however I am failing to do it dynamically.

Thanks

For any given ball you can either catch it (if possible), or drop it and lose a "life". In order to decide if you should sacrifice a life for any given ball, create a decision tree in which you either catch the ball or drop the ball. Your tree will fan out, creating 2^n subproblems. At each level of the tree, you will have at maximum k unique subproblems, since you reach that point with anywhere from 1 to k lives. Create a matrix with the best score you can get for the ith ball with k lives left if you catch it. You can then reference those values to avoid excess work. When k and the number of balls are small, you can use dynamic programming to examine all possible combinations of catches/drops in a manageable amount of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM