[Solution] Rocket Pack CodeChef Solution 2022
Problem
Chef's robot starts from the coordinate and wishes to reach to the coordinate .
The initial energy of the robot is . There are batteries
kept at some of the coordinates such that picking up the battery
costs units, and, on picking the battery
, the current energy of the robot becomes .
Note that one coordinate can have multiple batteries
but the robot can pick at most one battery
out of those.
For example, if the robot reaches a certain coordinate with energy and there are two batteries with energy and on that coordinate, the robot may choose at most one battery out of these. On choosing the first battery, the energy of the robot becomes (not ).
The robot can walk in all the four directions as follows:
- Move up: Moving from coordinate to , the robot loses unit of energy.
- Move down: Moving from coordinate to , the robot gains unit of energy.
- Move right: Moving from coordinate to , the robot loses unit of energy.
- Move left: Moving from coordinate to , the robot gains unit of energy.
Find the minimum cost to reach if the robot maintains a non-negative energy throughout the journey (including the destination).
Input Format
- The first line of input contains a single integer , the number of test cases. The description of the test cases follows.
- Each test cases consists of two lines of input.
- The first line contains three positive integers and , the coordinates of the destination and the number of batteries.
- Next lines have four space-separated integers each: . The line indicates that the coordinate has a
battery
with cost and energy .
- The input data guarantees that the robot can reach the destination.
Output Format
For each test case, output a single integer, the minimum cost from to .
Explanation:
Test case : Use the first battery with cost . Thus, at coordinate , the energy of the robot becomes units. The robot can traverse the path :. It can be shown that the robot cannot reach the destination in less than units cost.
Test case :
- Use the battery: At coordinate , robot picks the battery and the energy of the robot becomes units. The robot can move to coordinate using this energy. On reaching , robot's energy is .
- Use the battery: At coordinate , robot picks the battery and the energy of the robot becomes unit. The robot can move from to and gain unit of energy. Thus, it has units of energy now. It can now move from to using this energy.
- Use the battery: At coordinate , robot picks the battery and the energy of the robot becomes units. The robot can now move from to .
No comments:
Post a Comment