#P10234. [yLCPC2024] B. 找机厅
[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 matrix with rows and columns. Denote the cell in row and column as (, ).
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 , your next step can only go to one of the following four cells: , , , .
- At any time, you cannot move out of the matrix, i.e. your position must always satisfy , .
- 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 can only move to a cell containing , and vice versa.
Each step costs one unit of time. You need to reach from 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 , indicating the number of test cases. For each test case:
The first line contains two integers (), representing the number of rows and columns of the matrix.
The next lines each contain a string of length consisting only of characters 0 and 1, representing the matrix.
It is guaranteed that, within each test case, the sum of all and the sum of all do not exceed .
Output Format
For each test case, output one or two lines:
If it is impossible to reach from , output one line containing to indicate there is no solution.
Otherwise, output according to the following rules:
- The first line outputs an integer , the minimum time needed to go from the top-left corner to the bottom-right corner.
- The second line outputs a string of length consisting only of characters
LRUD, representing the movement path you provide:- If the -th move goes from to , then .
- If the -th move goes from to , then .
- If the -th move goes from to , then .
- If the -th move goes from to , then .
The length of the string on the second line must be exactly the minimum time 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