#P9425. [蓝桥杯 2023 国 B] AB 路线
[蓝桥杯 2023 国 B] AB 路线
Problem Description
There is a maze consisting of cells. Each cell contains the letter A or B. Xiao Lan starts at the top-left cell of the maze, and his goal is to reach the bottom-right cell. In each step, he may move to an adjacent cell in one of the four directions: up, down, left, or right.
For special reasons, Xiao Lan's route must first go through A cells, then B cells, then A cells, then B cells, and so on, alternating repeatedly.
Please compute the minimum number of steps Xiao Lan needs to take to reach the bottom-right cell.
Note that the number of cells visited along the route does not have to be a multiple of . That is, the last segment of A or B cells may contain fewer than cells. The starting cell is guaranteed to be an A cell.
For example, when , the following routes are valid:
AA
AAAB
AAABBBAAABBB
The following routes are invalid:
ABABAB
ABBBAAABBB
AAABBBBBBAAA
Input Format
The first line contains three integers , , and .
The next lines each contain characters (A or B), representing the type of each cell.
Output Format
Output one integer, the minimum number of steps. If it is impossible to reach the bottom-right cell, output .
4 4 2
AAAB
ABAB
BBAB
BAAA
8
Hint
Sample Explanation
The directions for each step are: down, right, down, right, up, right, down, down. The route sequence is: AABBAABBA.
Constraints
- For of the testdata, .
- For another of the testdata, .
- For of the testdata, , .
14th Lanqiao Cup Software Competition Finals, C/C++ University Group B, Problem G.
Translated by ChatGPT 5