#P16658. [GKS 2018 #G] Cave Escape
[GKS 2018 #G] Cave Escape
Problem Description
Mr. Raven is stuck in a cave represented by a matrix of rows and columns, where rows are numbered from to from top to bottom, and columns are numbered from to from left to right. The cell at the i-th row and the j-th column is denoted by . Mr. Raven is currently at the cell , and the exit of the cave is located at the cell .
Some cells of the cave may contain obstacles. Mr. Raven cannot step into a cell that has an obstacle.
Other cells may contain traps. The first time that Mr. Raven enters a cell with a trap, he must spend a number of energy points equal to the strength of the trap. If he has fewer energy points than needed, he cannot enter the cell.
Moreover, some other cells may contain potions. The first time that Mr. Raven enters a cell with a potion, he gains energy points equal to the strength of the potion.
Mr. Raven initially has energy points. He can move between cells that share an edge (not just a corner). On the exit cell, Mr. Raven can choose not to exit the cave and continue to explore the cave if he wants to. Can you help him find the maximum number of energy points he can have when he exits the cave, if it is possible to do so?
Input Format
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of one line with seven integers , , , , , and as described above. The i-th of the next lines describes the i-th row of the cave. Each line consists of integers ; the j-th of these represents the cell in the j-th column of the i-th row. Each can be one of the following
- : represents an empty cell.
- : represents a cell with an obstacle.
- an integer between and (both inclusive): represents a trap with strength .
- an integer between and (both inclusive): represents a potion with strength .
Output Format
For each test case, output one line containing Case #x: y, where x is the test case number (starting from ) and y is the maximum energy points that Mr. Raven can have when he reaches the exit of the cave. If it is not possible for Mr. Raven to reach the exit, output .
2
4 4 100 1 1 4 4
0 0 0 0
0 0 0 0
0 0 0 -100000
0 0 -100000 0
2 2 100 1 1 2 2
0 0
0 0
Case #1: -1
Case #2: 100
1
8 8 250 7 1 1 7
-100000 -100000 -100000 -100000 -100000 -100000 0 -100000
-100000 0 -100000 0 -400 0 0 -100000
-100000 100 -300 0 -100000 -300 -100000 -100000
-100000 0 -100000 500 -100000 250 0 -100000
-100000 -200 -100000 -100000 -100000 -100000 -100 -100000
-100000 0 -100000 0 0 50 50 -100000
0 0 -100 0 -100000 50 -100000 -100000
-100000 -100000 -100000 -100000 -100000 -100000 -100000 -100000
Case #1: 250
Hint
In Sample Case #1, it is not possible for Mr. Raven to reach the exit.
In Sample Case #2, there are no traps and no potions, which means Mr. Raven can reach exit and no matter what path he follows his energy will stay unchanged.
Limits
.
.
.
.
.
.
.
.
.
, for all i, j.
At most cells have . (There are at most traps.)
. (Mr. Raven's initial position is an empty cell.)
. (The cell with the exit is empty.)
Small dataset (Test set 1 - Visible)
There are no cells that have . (There are no potions in the cave.)
Large dataset (Test set 2 - Hidden)
No additional constraints.