atcoder#ABC253B. [ABC253B] Distance Between Tokens
[ABC253B] Distance Between Tokens
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.
2 3
--o
o--
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.
5 4
-o--
----
----
----
-o--
4