Gudi and the Magical Orbs
Practice
3.9 (55 votes)
Dynamic programming
Medium
Two dimensional
Problem
77% Success 2314 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Moving ahead, Gudi now enters a room where the floor is divided into N x M square tiles, forming a grid with N rows and M columns.
Each square in the grid contains a number of Magical Orbs that Gudi has to absorb if she steps on it. Each Orb increases her Kii power, which is needed to fight the demons. Gudi enters the room on (1, 1) with Zero Kii power and has to makes her way to the exit gate on (N, M), absorbing the Orbs on the way. From the current cell, she can move only to the adjacent cell in East, South or South-East direction i.e. from (i, j) to either (i, j+1) , (i+1, j) or (i+1, j+1).
However, her capacity for the Orbs is limited. If she absorbs more than K Orbs, she will explode with the large amount of Kii energy! Help her find a way to absorb as any many Orbs as she can, without exploding.

Input
The first line contains T. T testcases follow.
First line of each testcase contains 3 space-separated integers, N, M, K.
Each of the following N lines contain M space-separated integers, describing the grid.

Output
Print the maximum number of Orbs that she can absorb without exploding or "-1" (without the quotes) if it can not be done i.e. if there does not exist such a path. Print the answer to each testcase in a new line.

Constraints:
\( 1 \le T \le 10\)
\( 1 \le N,M \le 100\)
\( 1 \le K \le 500\)
1 ≤ Values in the Grid ≤ 50

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
AlgorithmsDynamic Programming2D dynamic programming
Points:30
7 votes
Tags:
2D dynamic programmingAlgorithmsDynamic Programming
Points:30
5 votes
Tags:
AlgorithmsDynamic ProgrammingMediumTwo dimensional