

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
行 列のマス目があり、そのうち二つの異なるマスに駒が置かれています。
マス目の状態は 個の長さ の文字列 で表されます。 o
ならば 行目 列目のマスに駒が置かれていることを、 -
ならばそのマスには駒が置かれていないことを表します。なお、 は文字列 の 文字目を指します。
一方の駒をマス目の外側に出ないように上下左右の隣接するマスに動かすことを繰り返すとき、もう一方の駒と同じマスに移動させるためには最小で何回動かす必要がありますか?
制約
- は整数
- は
o
および-
のみからなる長さ の文字列 -
o
となる整数 の組がちょうど二つ存在する
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを出力せよ。
入力例 1Copy
2 3 --o o--
出力例 1Copy
3
行目 列目に置かれている駒を 下 左 左 と移動すると 回でもう一方の駒と同じマスに移動させることができます。 回以下で移動させることはできないので、 を出力します。
入力例 2Copy
5 4 -o-- ---- ---- ---- -o--
出力例 2Copy
4
Score : points
Problem Statement
There is a grid with horizontal rows and vertical columns, in which two distinct squares have a piece.
The state of the squares is represented by strings of length . o
means that there is a piece in the square at the -th row from the top and -th column from the left; -
means that the square does not have a piece. Here, denotes the -th character of the string .
Consider repeatedly moving one of the pieces to one of the four adjacent squares. It is not allowed to move the piece outside the grid. How many moves are required at minimum for the piece to reach the square with the other piece?
Constraints
- and are integers.
- is a string of length consisting of
o
and-
. - There exist exactly two pairs of integers such that
o
.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
2 3 --o o--
Sample Output 1Copy
3
The piece at the -st row from the top and -rd column from the left can reach the square with the other piece in moves: down, left, left. Since it is impossible to do so in two or less moves, should be printed.
Sample Input 2Copy
5 4 -o-- ---- ---- ---- -o--
Sample Output 2Copy
4