#P1699. Bucket Brigade
Bucket Brigade
Bucket Brigade
The farm is ablaze and the cows are on a mission to form a water bucket relay to douse the flames! The farm is represented by a 10×10 grid of characters. Each cell may be one of the following:
.
— an empty fieldB
— the barn (which is on fire)L
— a lakeR
— a large rock
Cows can be placed only on empty cells (i.e. cells containing .
). They must form a connected relay (via north, south, east, or west adjacent cells) such that one cow is adjacent to the lake (allowing it to fetch water) and one cow is adjacent to the barn (so that the water can be used to extinguish the fire). Note that the barn and the lake are guaranteed not to be adjacent.
If the lake and barn lie on the same row and the rock is positioned strictly between them (or similarly, on the same column with the rock strictly between), then the rock will force the relay to take a detour requiring one extra cow.
More formally, let the Manhattan distance between the lake and barn be \(d = |r_B - r_L| + |c_B - c_L|\). The answer is calculated as follows:
[
\text{answer} = \begin{cases}
d + 1, & \text{if } (r_B = r_L \text{ and } \min(c_B,c_L) < c_R < \max(c_B,c_L))
& \text{or } (c_B = c_L \text{ and } \min(r_B,r_L) < r_R < \max(r_B,r_L)) \
d - 1, & \text{otherwise.}
\end{cases}
]
Your task is to determine the minimal number of empty cells (i.e. the number of cows required) needed to form a valid bucket brigade.
inputFormat
The input consists of 10 lines, each containing exactly 10 characters. These characters will be one of .
, B
, L
, or R
, representing the empty field, barn, lake, or rock respectively.
outputFormat
Output a single integer—the minimal number of empty cells that need to be occupied by cows in order to form a valid bucket brigade between the lake and the barn.
sample
..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........
7