Distant Pastures
Time limit1sMemory limit128 MB
Each grid cell has one of two grass types; moving between adjacent cells costs A if types match and B otherwise. Find the largest shortest-path distance over all pairs of cells.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, BFS, Implementation
- Solved
- No attempts yet
Problem
Farmer John's farm is an grid of pastures. Each pasture grows one of two kinds of grass, written with the characters ( and ). For example, the farm might look like this:
(())
)()(
)(((
))))
When Bessie the cow moves to an adjacent pasture (one step north, south, east, or west), the move takes units of time if the two pastures grow the same kind of grass, or units of time if they grow different kinds. Whenever Bessie travels from one pasture to another, she always follows a route whose total time is as small as possible.
Consider the minimum travel time between every pair of pastures. Output the largest of these minimum times.
Input
- The first line contains three integers , , and with and .
- Each of the next lines contains a string of parentheses. Together these lines describe the grid of pastures.
Output
Print a single integer: the largest possible minimum travel time between any pair of pastures, given that Bessie always takes a fastest route.
Notes
Think of the pastures as the vertices of a graph whose edges join orthogonally adjacent pastures, each weighted or . The requested value is the largest shortest-path distance over all pairs of vertices (the weighted diameter of the grid graph).