#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 NN rows and MM columns, where rows are numbered from 11 to NN from top to bottom, and columns are numbered from 11 to MM from left to right. The cell at the i-th row and the j-th column is denoted by (i,j)(i, j). Mr. Raven is currently at the cell (SR,SC)(S_R, S_C), and the exit of the cave is located at the cell (TR,TC)(T_R, T_C).

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 EE 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, TT. TT test cases follow. Each test case consists of one line with seven integers NN, MM, EE, SRS_R, SCS_C, TRT_R and TCT_C as described above. The i-th of the next NN lines describes the i-th row of the cave. Each line consists of MM integers VijV_{ij}; the j-th of these represents the cell in the j-th column of the i-th row. Each VijV_{ij} can be one of the following

  • 00: represents an empty cell.
  • 100000-100000: represents a cell with an obstacle.
  • an integer between 99999-99999 and 1-1 (both inclusive): represents a trap with strength Vij-V_{ij}.
  • an integer between 11 and 9999999999 (both inclusive): represents a potion with strength VijV_{ij}.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 11) 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 1-1.

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

1T1001 \le T \le 100.

1N1001 \le N \le 100.

1M1001 \le M \le 100.

0E1000000 \le E \le 100000.

1SRN1 \le S_R \le N.

1SCM1 \le S_C \le M.

1TRN1 \le T_R \le N.

1TCM1 \le T_C \le M.

(SR,SC)(TR,TC)(S_R, S_C) \ne (T_R, T_C).

100000Vij<100000-100000 \le V_{ij} < 100000, for all i, j.

At most 1515 cells have 100000<Vij<0-100000 < V_{ij} < 0. (There are at most 1515 traps.)

VSRSC=0V_{S_R S_C} = 0. (Mr. Raven's initial position is an empty cell.)

VTRTC=0V_{T_R T_C} = 0. (The cell with the exit is empty.)

Small dataset (Test set 1 - Visible)

There are no cells that have Vij>0V_{ij} > 0. (There are no potions in the cave.)

Large dataset (Test set 2 - Hidden)

No additional constraints.