P1825 [USACO11OPEN] Corn Maze S

Description

This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides work in both directions: a cow can slide from the slide's start to the end instantly, or from the end to the start. If a cow steps on a space that hosts either end of a slide, she must use the slide. The outside of the corn maze is entirely corn except for a single exit. The maze can be represented by an N x M (2

Input Format

Line 1: Two space separated integers: N and M Lines 2..N+1: Line i+1 describes row i of the maze: M characters (no spaces)

Output Format

Line 1: An integer, corresponding to the shortest time that Bessie needs to exit the maze.

Explanation/Hint

Sample Explanation: ```plain ###=## #.W.## #.#### #.@W## ###### ``` The endpoints of the only slide are marked with capital letters W. The optimal strategy (which takes 3 units of time) is to: Move right to the slide endpoint using 1 unit of time, transport to the other endpoint using 0 units of time, move right using 1 unit of time, and move up to the exit using 1 unit of time.