#P1699. [USACO19OPEN] Bucket Brigade B
[USACO19OPEN] Bucket Brigade B
Problem Description
A fire has broken out on the farm, and the cows are rushing to put it out!
The farm can be described by a grid of characters like this:
..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........
The character B represents the burning barn. The character L represents a lake, and the character R represents a huge rock on the farm.
The cows want to form a “bucket passing line” along a path from the lake to the barn, so that they can pass buckets of water along this path to help put out the fire. A bucket can be passed between two cows if they are adjacent in one of the four directions (north, south, east, west). This is also true for the cow next to the lake—the cows can only take water from the lake when they are directly adjacent to it. Similarly, the cows can only use water to put out the barn fire when they are directly adjacent to the barn.
Please help find the minimum number of . cells that the cows need to occupy in order to form such a “bucket passing line”.
The cows cannot stand on the cell containing the rock. Also, it is guaranteed that the barn and the lake are not adjacent.
Input Format
The input contains lines, each with characters, describing the layout of the farm. It is guaranteed that the grid contains exactly one B, one L, and one R.
Output Format
Output one integer: the minimum number of cows needed to form a feasible bucket passing line.
..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........
7
Hint
Sample Explanation 1
In this example, the following is a feasible plan that uses the minimum number of cows ():
..........
..........
..........
..B.......
..C.......
..CC.R....
...CCC....
.....C....
.....L....
..........
Translated by ChatGPT 5