#P10234. [yLCPC2024] B. 找机厅

    ID: 11490 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>洛谷原创Special Judge广度优先搜索 BFS洛谷月赛

[yLCPC2024] B. 找机厅

Background

Fusu is heading out to play mai!

But the mall is way too complicated, and she got lost inside. Having already gotten lost once in the subway station, Fusu stares at the mall map and still cannot figure out where to go. Can you help her?

Problem Description

You are given a 0101 matrix with nn rows and mm columns. Denote the cell in row ii and column jj as (i,j)(i, j) (1in1 \leq i \leq n, 1jm1 \leq j \leq m).

You need to start from the top-left corner of the matrix and reach the bottom-right corner. The movement rules are as follows:

  • If you are at cell (i,j)(i, j), your next step can only go to one of the following four cells: (i1,j)(i - 1, j), (i+1,j)(i + 1, j), (i,j1)(i, j - 1), (i,j+1)(i, j + 1).
  • At any time, you cannot move out of the matrix, i.e. your position (i,j)(i, j) must always satisfy 1in1 \leq i \leq n, 1jm1 \leq j \leq m.
  • If you want to move from one cell to another, besides meeting the requirements above, you must also ensure that the digits in the two cells are different. That is, a cell containing 00 can only move to a cell containing 11, and vice versa.

Each step costs one unit of time. You need to reach (n,m)(n, m) from (1,1)(1, 1) in the shortest time. In addition to the shortest time, you must also provide one feasible movement path that achieves this shortest time.

Input Format

This problem contains multiple test cases within a single test point. The first line contains a positive integer TT, indicating the number of test cases. For each test case:

The first line contains two integers n,mn, m (1n,m2×1031 \leq n, m \leq 2 \times 10^3), representing the number of rows and columns of the matrix.
The next nn lines each contain a string of length mm consisting only of characters 0 and 1, representing the matrix.

It is guaranteed that, within each test case, the sum of all nn and the sum of all mm do not exceed 2×1032 \times 10^3.

Output Format

For each test case, output one or two lines:

If it is impossible to reach (n,m)(n, m) from (1,1)(1, 1), output one line containing 1-1 to indicate there is no solution.

Otherwise, output according to the following rules:

  • The first line outputs an integer tt, the minimum time needed to go from the top-left corner to the bottom-right corner.
  • The second line outputs a string ss of length tt consisting only of characters L R U D, representing the movement path you provide:
    • If the ii-th move goes from (i,j)(i, j) to (i1,j)(i - 1, j), then si=Us_i = \texttt{U}.
    • If the ii-th move goes from (i,j)(i, j) to (i+1,j)(i + 1, j), then si=Ds_i = \texttt{D}.
    • If the ii-th move goes from (i,j)(i, j) to (i,j1)(i, j-1), then si=Ls_i = \texttt{L}.
    • If the ii-th move goes from (i,j)(i, j) to (i,j+1)(i, j + 1), then si=Rs_i = \texttt{R}.

The length of the string on the second line must be exactly the minimum time tt given in the first line. If there are multiple valid solutions, you may output any one of them.

2
2 2
01
11
2 2
01
10
-1
2
RD

Hint

Translated by ChatGPT 5