CCCGuide

Problem J5: Harvest Waterloo

2024 · J5 · Original problem · DMOJ

Problem Description

There is a wildly popular new harvest simulation game called Harvest Waterloo. The game is played on a rectangular pumpkin patch which contains bales of hay and pumpkins of different sizes. To begin the game, a farmer is placed at the location of a pumpkin.

The farmer harvests all pumpkins they can reach by moving left, right, up, and down throughout the patch. The farmer cannot move diagonally. The farmer can also not move through a bale of hay nor move outside of the patch.

Your job is to determine the total value of all the pumpkins harvested by the farmer. A small pumpkin is worth $1\$1, a medium pumpkin is worth $5\$5, and a large pumpkin is worth $10\$10 dollars.

Input Specification

The first line of input is an integer R>0R > 0 which is the number of rows within the patch.

The second line of input is an integer C>0C > 0 which is the number of columns within the patch.

The next RR lines describe the patch. Each line will contain CC characters and each character will either represent a pumpkin size or a bale of hay: S for a small pumpkin, M for a medium pumpkin, L for a large pumpkin, or * for a bale of hay.

The next line of input is an integer AA where 0≤A<R0 \leq A < R, and the last line of input is an integer BB where 0≤B<C0 \leq B < C. Row AA and column BB is the starting location of the farmer and the top-left corner of the patch is row 00 and column 00.

The following table shows how the available 1515 marks are distributed:

MarksDescriptionBound
11The patch is small and there are no bales of hay.R×C≤100R \times C \leq 100
44The patch is small and the bales of hay divide the entire patch into rectangular areas.R×C≤100R \times C \leq 100
55The patch is small and the bales of hay can be anywhere.R×C≤100R \times C \leq 100
55The patch is large and the bales of hay can be anywhere.R×C≤100 000R \times C \leq 100\,000

Output Specification

Output the integer, VV, which is the total value in dollars of all the pumpkins harvested by the farmer.

Sample Input 1

6 
6
**LMLS
S*LMMS
S*SMSM
******
LLM*MS
SSL*SS
5
1

Output for Sample Input 1

37

A 6 by 6 pumpkin patch. A person is standing in the sixth row and second column. A 2 by 3 group consisting of the first three pumpkins in the fifth and sixth rows is highlighted. This group is bordered by two edges of the patch and bales of hay, and it contains 2 small, 1 medium and 3 large pumpkins.

Explanation of Output for Sample Input 1

Starting at row 5 and column 1, the farmer can reach the 6 pumpkins in the highlighted area. They harvest 2 small pumpkins, 1 medium pumpkin, and 3 large pumpkins. The total value in dollars of this harvest is 2×1+1×5+3×10=372 \times 1 + 1 \times 5 + 3 \times 10 = 37.

Sample Input 2

6
6
**LMLS
S*LMMS
S*SMSM
***SLL
LLM*MS
SSL*SS
2
4

Output for Sample Input 2

88

A 6 by 6 pumpkin patch. A person is standing in the third row and fifth column. A group formed by the last four pumpkins in the first three rows, the last three in the fourth row, and the last two in the fifth and six rows is highlighted. The group is bordered by three edges of the patch and bales of hay, and it contains 8 small, 6 medium and 5 large pumpkins.

Explanation of Output for Sample Input 2

Starting at row 22 and column 44, the farmer can reach the 1919 pumpkins in the highlighted area. They harvest 88 small pumpkins, 66 medium pumpkin, and 55 large pumpkins. The total value in dollars of this harvest is 8×1+6×5+5×10=888 \times 1 + 6 \times 5 + 5 \times 10 = 88.