PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000429. Leo vách đá (Rock Climbing)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 RR rows and CC columns. An extra row of CC exit cells E lies immediately above the cliff, and an extra row of CC 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 rr and column cc has an integer Xr,cX_{r,c}. 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 Xr,cX_{r,c}; a negative value therefore increases his energy by −Xr,c-X_{r,c}. 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 R,CR,C.

The second line contains CC space-separated E characters.

The following RR lines each contain CC integers Xr,1,Xr,2,…,Xr,CX_{r,1},X_{r,2},\ldots,X_{r,C}, in order from top to bottom.

The final line contains CC space-separated S characters.

Output

Print one integer: the minimum initial energy required.

Subtasks

Subtask 1 (20 points)

  • 1≤R,C≤41\le R,C\le4.
  • −9≤Xr,c≤9-9\le X_{r,c}\le9 for 1≤r≤R1\le r\le R, 1≤c≤C1\le c\le C.
  • The exit and start rows contain exactly CC E and CC S characters, respectively.

Subtask 2 (30 points)

  • 1≤R,C≤151\le R,C\le15.
  • 0≤Xr,c≤90\le X_{r,c}\le9 for 1≤r≤R1\le r\le R, 1≤c≤C1\le c\le C.
  • The exit and start rows contain exactly CC E and CC S characters, respectively.

Subtask 3 (50 points)

  • 1≤R,C≤151\le R,C\le15.
  • −9≤Xr,c≤9-9\le X_{r,c}\le9 for 1≤r≤R1\le r\le R, 1≤c≤C1\le c\le C.
  • The exit and start rows contain exactly CC E and CC S characters, 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 99 and 88 to cross the bottom two cliff rows before gaining energy, so at least 1717 is needed. Starting with 1717, climb column 1 to row 3, step sideways and back to build up 66 energy, then enter the cells costing 55 and 11 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 00, the start row 1414, and columns from 11 through 55. With initial energy 3232, start at (14,1)(14,1) and climb column 1 to (4,1)(4,1). Move through (4,2)(4,2), (4,3)(4,3), (5,3)(5,3), (5,4)(5,4) and (5,5)(5,5), then climb column 5 to the exit. The remaining energy at (4,1)(4,1) is 99, at (5,5)(5,5) it is 44, and at the exit it is zero. No valid route is possible with 3131 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