#BS0000429. Leo vách đá (Rock Climbing)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Leo vách đá (Rock Climbing)
Rock Climbing
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Peter wants to climb a seaside cliff from its base to its top. The cliff has rows and columns. An extra row of exit cells E lies immediately above the cliff, and an extra row of start cells S lies immediately below it. Peter may begin at any start cell and finish at any exit cell.
Each cliff cell in row and column has an integer . A move takes Peter to an adjacent cell sharing an edge (up, down, left or right) within the board, including the start and exit rows. Upon entering a cliff cell, his energy decreases by ; a negative value therefore increases his energy by . Entering S or E costs zero. Revisiting cells, including start cells, is allowed, and the cost applies on every entry.
His energy must remain nonnegative throughout the climb. Determine the minimum initial energy that enables him to reach an exit.
Input
The first line contains two integers .
The second line contains space-separated E characters.
The following lines each contain integers , in order from top to bottom.
The final line contains space-separated S characters.
Output
Print one integer: the minimum initial energy required.
Subtasks
Subtask 1 (20 points)
- .
- for , .
- The exit and start rows contain exactly
EandScharacters, respectively.
Subtask 2 (30 points)
- .
- for , .
- The exit and start rows contain exactly
EandScharacters, respectively.
Subtask 3 (50 points)
- .
- for , .
- The exit and start rows contain exactly
EandScharacters, respectively.
Examples
Example 1
Input
5 5
E E E E E
1 2 3 4 5
5 4 3 2 1
-2 -2 -2 -2 -2
8 8 8 8 8
9 9 9 9 9
S S S S S
Output
17
Explanation
Peter must first pay and to cross the bottom two cliff rows before gaining energy, so at least is needed. Starting with , climb column 1 to row 3, step sideways and back to build up energy, then enter the cells costing and before reaching the exit with zero energy.
Example 2
Input
13 5
E E E E E
1 1 1 1 1
1 1 1 2 1
9 9 9 9 1
1 1 1 9 1
1 9 1 1 1
1 9 9 9 9
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
9 6 7 8 9
-5 9 -7 9 -5
6 0 7 0 5
S S S S S
Output
32
Explanation
Number the exit row , the start row , and columns from through . With initial energy , start at and climb column 1 to . Move through , , , and , then climb column 5 to the exit. The remaining energy at is , at it is , and at the exit it is zero. No valid route is possible with or less initial energy.
Adapted from Virginia Tech High School Programming Contest 2016, Problem J, Peter Steele. CC BY-SA 3.0 as listed in Kattis metadata mirrored by VJudge. Original contest · CC BY-SA 3.0