This page is still under construction.

Parts of this page are still being built. What you see may change.

Distant Pastures

Time limit1sMemory limit128 MB

Summary
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 N×NN \times N 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 AA units of time if the two pastures grow the same kind of grass, or BB 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 NN, AA, and BB with 1≤N≤301 \le N \le 30 and 0≤A,B≤1060 \le A, B \le 10^6.
  • Each of the next NN lines contains a string of NN parentheses. Together these lines describe the N×NN \times N 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 AA or BB. The requested value is the largest shortest-path distance over all pairs of vertices (the weighted diameter of the grid graph).

Examples2

  1. Example 1

    Input
    3 1 2
    (((
    ()(
    (()
    
    Expected output
    5
    
  2. Example 2

    Input
    2 2 5
    ()
    )(
    
    Expected output
    10