#P2385. [USACO07FEB] Bronze Lilypad Pond B

[USACO07FEB] Bronze Lilypad Pond B

Description

To entertain and exercise the cows, Farmer John built a beautiful pond. This rectangular pond is divided into MM rows and NN columns (1M,N301 \le M, N \le 30). Some cells are surprisingly sturdy lilypads, some are rocks, and the rest are beautiful, pure, deep-blue water.

Bessie is practicing ballet. She stands on a lilypad and wants to jump to another lilypad. She can only jump from a lilypad to another lilypad; she cannot jump onto water or rocks.

Bessie’s steps are like a knight’s moves in chess: each time she either moves horizontally M1M_1 squares (1M1301 \le M_1 \le 30) and then vertically M2M_2 squares (1M2301 \le M_2 \le 30, M1M2M_1 \ne M_2), or vertically M1M_1 squares and then horizontally M2M_2 squares. At most, Bessie has eight possible movement directions.

Given the pond layout and Bessie’s jump lengths, compute the minimum number of jumps needed for Bessie to go from the start to the destination. It is guaranteed that the destination in the input is reachable.

Input Format

The first line contains four space-separated integers: MM, NN, M1M_1, and M2M_2.

The second line through line M+1M+1: for each ii from 11 to MM, line i+1i+1 contains NN space-separated integers describing row ii of the pond: 00 for water, 11 for lilypad, 22 for rock, 33 for Bessie’s starting cell, and 44 for Bessie’s destination.

Output Format

The first line contains the minimum number of moves from the start to the destination.

4 5 1 2
1 0 1 0 1
3 0 2 0 4
0 1 2 0 0
0 0 0 1 0
2

Hint

Translated by ChatGPT 5