Rayan Programming Contest 2024 - Selection (Codeforces Round 989, Div. 1 + Div. 2)
C
Trapped in the Witch's Labyrinth
Codeforces
1400
原题
← BD →
翻译中…

In the fourth labor of Rostam, the legendary hero from the Shahnameh, an old witch has created a magical maze to trap him. The maze is a rectangular grid consisting of $n$ rows and $m$ columns. Each cell in the maze points in a specific direction: up, down, left, or right. The witch has enchanted Rostam so that whenever he is in a cell, he will move to the next cell in the direction indicated by that cell.

翻译中…

image

翻译中…

If Rostam eventually exits the maze, he will be freed from the witch's enchantment and will defeat her. However, if he remains trapped within the maze forever, he will never escape.

翻译中…

The witch has not yet determined the directions for all the cells. She wants to assign directions to the unspecified cells in such a way that the number of starting cells from which Rostam will be trapped forever is maximized. Your task is to find the maximum number of starting cells which make Rostam trapped.

翻译中…

Input

翻译中…

The first line of the input contains an integer $t$ ($1 \leq t \leq 10^4$), the number of test cases.

翻译中…

For each test case:

翻译中…
  • The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 1000$), representing the number of rows and columns in the maze.
  • Each of the next $n$ lines contains a string of $m$ characters representing the directions in the maze. Each character is one of the following:
  • U (up)
    • D (down)
    • L (left)
    • R (right)
    • ? (unspecified direction)
翻译中…

It's guaranteed that the sum of $n \cdot m$ over all test cases is at most $10^6$.

翻译中…

Output

翻译中…

For each test case, print a single integer, the maximum number of starting cells from which Rostam will be trapped forever after assigning directions to the unspecified cells optimally.

翻译中…

Note

翻译中…

In the first test case, all of the cells will be good no matter what you do.

翻译中…

In the second test case, if you assign the ?s like the picture below, all of the cells will be bad:

翻译中…

image

翻译中…

In the third test case, if you assign the ?s like the picture below, you will have $5$ bad cells (red-shaded cells):

翻译中…

image

翻译中…

样例

Input #1
3
3 3
UUU
L?R
DDD
2 3
???
???
3 3
?U?
R?L
RDL
Output #1
0
6
5
InputCopy OutputCopy
题目信息
题号
2034C
时间限制
3 s
内存限制
256 MB
标签
构造
DFS 及类似
图论
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。