#P10294. Harvest Waterloo: Pumpkin Harvest Simulation

    ID: 12295 Type: Default 1000ms 256MiB

Harvest Waterloo: Pumpkin Harvest Simulation

Harvest Waterloo: Pumpkin Harvest Simulation

You are given a grid representing a rectangular pumpkin field in the game Harvest Waterloo. The field consists of hay bales and pumpkins of different sizes. The farmer starts at a specified position, which is always located on a pumpkin cell. The farmer can move in four directions: left, right, up, and down. He cannot move diagonally, pass through hay bales, or leave the field.

Each pumpkin has a monetary value: a small pumpkin is worth \( $1 $\), a medium pumpkin is worth \( $5 $\), and a large pumpkin is worth \( $10 $\). When the farmer steps on a pumpkin cell, he harvests that pumpkin. Your task is to calculate the total value of the pumpkins the farmer can harvest by moving over all reachable pumpkin cells starting from his initial position.

The field is described by a grid. Each cell is either a hay bale denoted by H or a pumpkin cell containing one of the values 1, 5, or 10 (as strings). The starting position is given as a pair of integers which indicate the row and column (0-indexed) of the grid. It is guaranteed that the starting cell contains a pumpkin.

inputFormat

The first line contains two integers n and m, the number of rows and columns of the grid.

The second line contains two integers r and c which represent the starting row and column (0-indexed) of the farmer. This cell is guaranteed to contain a pumpkin.

The following n lines each contain m tokens separated by spaces. Each token is either H (indicating hay) or one of 1, 5, or 10 (indicating a pumpkin of the corresponding value).

outputFormat

Output a single integer, the total value of all pumpkins reachable by the farmer.

sample

3 3
0 0
1 5 H
H 10 5
1 H 1
22

</p>