#abc253b. B - Distance Between Tokens
B - Distance Between Tokens
Score : points
问题描述
存在一个具有 行(水平方向)和 列(垂直方向)的网格,在其中两个不同的格子内各有一个棋子。
格子的状态通过长度为 的 个字符串 来表示。若 o
,则表示在从上到下第 行、从左到右第 列的格子中存在一个棋子;若 -
,则表示该格子中没有棋子。这里, 表示字符串 中的第 个字符。
考虑反复将一个棋子移动到其四个相邻的格子之一。不允许将棋子移出网格范围。请问至少需要多少步才能使一个棋子到达另一个棋子所在的格子?
以上为通义千问 qwen-max 翻译,仅供参考。
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 1
2 3
--o
o--
Sample Output 1
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 2
5 4
-o--
----
----
----
-o--
Sample Output 2
4
update @ 2024/3/10 10:47:19