欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1391B.Fix You
Fix You
Consider a conveyor belt represented using a grid consisting of rows and columns. The cell in the -th row from the top and the -th column from the left is labelled .
Every cell, except , has a direction R (Right) or D (Down) assigned to it. If the cell is assigned direction R, any luggage kept on that will move to the cell . Similarly, if the cell is assigned direction D, any luggage kept on that will move to the cell . If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell , any luggage placed in this cell should eventually end up in the cell .
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains two integers (, ) — the number of rows and columns, respectively.
The following lines each contain characters. The -th character in the -th line, is the initial direction of the cell . Please note that C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Note
In the first case, just changing the direction of to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at , it first moves to and then to .
In the second case, we have no option but to change the first cells from D to R making the grid equal to RRRC.
Samples
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
1
3
9
0
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |