Bucket Brigade
InterviewTime limit2sMemory limit512 MB
On a fixed 10x10 grid with one barn, one lake, and one blocked rock, find the fewest empty squares to occupy so a chain of cows links the lake to the barn.
- Level
Medium5 of 10
- Topics
- BFS, Graph, Shortest path, Implementation
- Solved
- No attempts yet
Problem
A fire has broken out on the farm, and the cows are rushing to put it out.
The farm is described by a grid of characters like this:
..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........
The character 'B' marks the barn, which has just caught on fire. 'L' marks a lake, and 'R' marks the location of a large rock.
The cows want to form a "bucket brigade" by placing themselves along a path between the lake and the barn so that they can pass buckets of water along the path to extinguish the fire. A bucket can move between two cows if the cows are immediately adjacent in the north, south, east, or west directions. The same holds for a cow next to the lake: she can take a bucket of water from the lake only if she is immediately adjacent to the lake. Likewise, a cow can throw a bucket of water on the barn only if she is immediately adjacent to the barn.
Find the minimum number of '.' squares that must be occupied by cows to form a working bucket brigade.
A cow cannot be placed on the square containing the large rock, and the barn and the lake are guaranteed not to be immediately adjacent to each other.
Input
The input consists of 10 lines of 10 characters each, describing the layout of the farm. There is exactly one barn, one lake, and one rock.
Output
Print a single integer: the minimum number of cows needed to form a working bucket brigade.
Hint
In the example below, one possible solution uses the optimal number of cows (7):
..........
..........
..........
..B.......
..C.......
..CC.R....
...CCC....
.....C....
.....L....
..........