#P9425. [蓝桥杯 2023 国 B] AB 路线

    ID: 10582 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2023广度优先搜索 BFS蓝桥杯国赛

[蓝桥杯 2023 国 B] AB 路线

Problem Description

There is a maze consisting of N×MN \times M 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 KK A cells, then KK B cells, then KK A cells, then KK 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 KK. That is, the last segment of A or B cells may contain fewer than KK cells. The starting cell is guaranteed to be an A cell.

For example, when K=3K = 3, the following 33 routes are valid:

AA
AAAB
AAABBBAAABBB

The following 33 routes are invalid:

ABABAB
ABBBAAABBB
AAABBBBBBAAA

Input Format

The first line contains three integers NN, MM, and KK.

The next NN lines each contain MM 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 1-1.

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 20%20\% of the testdata, 1N,M41 \le N, M \le 4.
  • For another 20%20\% of the testdata, K=1K = 1.
  • For 100%100\% of the testdata, 1N,M10001 \le N, M \le 1000, 1K101 \le K \le 10.

14th Lanqiao Cup Software Competition Finals, C/C++ University Group B, Problem G.

Translated by ChatGPT 5