#P14014. [ICPC 2024 Nanjing R] 嘿,有看到我的袋鼠吗?
[ICPC 2024 Nanjing R] 嘿,有看到我的袋鼠吗?
Description
$\textbf{Please note the UNUSUAL MEMORY LIMIT of this problem.}$
After the great success in 2018, 2019, 2020, 2021, 2022, and 2023, the Nanjing University of Aeronautics and Astronautics (NUAA) will host the $\textit{International Collegiate Programming Contest}$ (ICPC) Nanjing regional for the seventh time in a row.
Team and team won the champion title for Tsinghua University in 2018 and 2019. In 2020, 2021, and 2022, team from Peking University won the three-peat champion titles. In 2023, another team from Peking University won the title. They also won the 46th ICPC World Champion, reclaiming the trophy for the EC region after 13 years!
This year, around teams are participating in the contest. At most gold medals, silver medals, and bronze medals will be awarded (note that these numbers are for reference only). We are looking forward to seeing the participants' outstanding performance! We also want to express our gratitude for the hard work done by all staff and volunteers for this contest. Thank you all for your great contribution to this contest!
:::align{center}

Photo taken in the 2023 ICPC Asia Nanjing Regional Contest :::
In the 2018 contest, problem K, , requires the contestants to construct an operation sequence for the game:
The puzzle is a grid with rows and columns (), and there are some (at least ) kangaroos standing in the puzzle. The player's goal is to control them to get together. There are some walls in some cells, and the kangaroos cannot enter the cells with walls. The other cells are empty. The kangaroos can move from an empty cell to an adjacent empty cell in four directions: up, down, left, and right.
There is exactly one kangaroo in every empty cell in the beginning, and the player can control the kangaroos by pressing the buttons U, D, L, R on the keyboard. The kangaroos will move simultaneously according to the button you press. The contestant needs to construct an operating sequence of at most steps consisting of U, D, L, R only to achieve the goal.
In the 2020 contest, problem A, , requires the contestants to construct an input map to hack the following code of the problem described before:
#include <bits/stdc++.h>
using namespace std;
string s = "UDLR";
int main()
{
srand(time(NULL));
for (int i = 1; i <= 50000; i++) putchar(s[rand() % 4]);
return 0;
}
Furthermore, in the 2021 contest (Problem A, , the 2022 contest (Problem A, , and the 2023 contest (Problem A, $\textbf{\textit{Cool, It's Yesterday Four Times More}})$, every year we have a problem related to the kangaroos! We would like to introduce all these problems to you, but if we do so every year, we may have a 500-page statement for one single problem in the 3024 contest. Therefore, we omit them this time. Besides, you may already have seen them in the practice contest.
Now, in the 2024 contest, as everyone expects, the kangaroo problem is back again! We don't know why problem setters are so obsessed with kangaroos, but the problem is as follows:
You are given a grid with rows and columns. There are some walls in some cells, and the kangaroos cannot enter the cells with walls. The other cells are empty and each contains a kangaroo. The kangaroos can move from an empty cell to an adjacent empty cell in four directions: up, down, left, and right.
You can control the kangaroos by pressing the buttons U, D, L, R on the keyboard. The kangaroos will move simultaneously according to the button you press. Specifically, for any kangaroo located in the cell on the -th row and the -th column, indicated by :
- Button U: it will move to if and is not a wall. Otherwise, it will stay in the same cell.
- Button D: it will move to if and is not a wall. Otherwise, it will stay in the same cell.
- Button L: it will move to if and is not a wall. Otherwise, it will stay in the same cell.
- Button R: it will move to if and is not a wall. Otherwise, it will stay in the same cell.
You are given an operating sequence consisting only of characters U, D, L, and R. The operations are performed infinitely according to the sequence. Specifically, if , the -th operation is ; Otherwise if , the -th operation is the same as the -th operation. For each , find the smallest integer such that after performing operations, at most cells will contain kangaroos.
Input Format
There is only one test case in each test file.
The first line contains three integers , , and (, , ), indicating the number of rows and columns of the grid, and the length of the operating sequence.
The second line contains a string ($s_i \in \{\text{`U'}, \text{`D'}, \text{`L'}, \text{`R'}\}$), indicating the operating sequence.
For the following lines, the -th line contains a binary string (). If then cell is empty; Otherwise if then cell is blocked and cannot be entered. It is guaranteed that there is at least one empty cell in the grid.
Output Format
Output lines, where the -th line contains an integer , indicating the minimum number of operations needed so that at most cells will contain kangaroos. If this is impossible, just output on this line.
3 3 6
ULDDRR
010
111
010
-1
4
2
1
0
0
0
0
0
3 3 6
ULDDRR
010
111
011
7
4
2
1
1
0
0
0
0
1 5 1
R
11111
4
3
2
1
0
京公网安备 11011102002149号