1391 B(a).Fix You(规律+构造)

B. Fix You

http://codeforces.com/contest/1391/problem/B

题面:

Consider a conveyor belt represented using a grid consisting of n n n rows and m m m columns. The cell in the i i i-th row from the top and the j j j-th column from the left is labelled ( i , j ) (i,j) (i,j).

Every cell, except ( n , m ) (n,m) (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell ( i , j ) (i,j) (i,j) is assigned direction R, any luggage kept on that will move to the cell ( i , j + 1 ) (i,j+1) (i,j+1). Similarly, if the cell ( i , j ) (i,j) (i,j) is assigned direction D, any luggage kept on that will move to the cell ( i + 1 , j ) (i+1,j) (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.

There is a counter at the cell ( n , m ) (n,m) (n,m) 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 ( i , j ) (i,j) (i,j), any luggage placed in this cell should eventually end up in the cell ( n , m ) (n,m) (n,m).

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 t t t ( 1 ≤ t ≤ 10 1 \le t \le 10 1t10). Description of the test cases follows.

The first line of each test case contains two integers n , m n, m n,m ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100, 1 ≤ m ≤ 100 1 \le m \le 100 1m100) — the number of rows and columns, respectively.

The following n n n lines each contain m m m characters. The j j j-th character in the i i i-th line, a i , j a_{i,j} ai,j is the initial direction of the cell ( i , j ) (i, j) (i,j). Please note that a n , m = a_{n,m}= an,m= 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.

Example

input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
output
1
3
9
0
Note

In the first case, just changing the direction of ( 2 , 3 ) (2,3) (2,3) to D is enough.

You can verify that the resulting belt is functional. For example, if we place any luggage at ( 2 , 2 ) (2,2) (2,2), it first moves to ( 3 , 2 ) (3,2) (3,2) and then to ( 3 , 3 ) (3,3) (3,3).

In the second case, we have no option but to change the first 3 3 3 cells from D to R making the grid equal to RRRC.

翻译:

考虑一个传输带conveyor belt 被表示为represented 使用一个表格组成consisting 由 n 行rows 和 m列columns 。顶部第 i 行和左侧第 j 列 的大院个被标记成 ( i , j ) (i,j) (i,j).

每一个单元格,除了 ( n , m ) (n,m) (n,m) ,都有方向 direction R和D被分配 assigned 。如果单元格 ( i , j ) (i,j) (i,j) 被分配为 方向R,任何行李 luggage 将继续移动向单元 $ (i,j+1) $ 。同样,如果单元格被分配为方向D,任何行李将继续移动到 ( i + 1 , j ) (i+1,j) (i+1,j)。如果任何时候,行李移动出格子,他将被视为丢失。

这里有一个柜台counter 在 ( n , m ) (n,m) (n,m)的单元格从这里所有行李都被取走picked。当且仅当if and only if 任何行李到达柜台 无论regardless 是从哪个单元格被放置最初,传送带都被叫做功能性functional 传送带。更正式formally的说,对于每一个单元格 ( i , j ) (i,j) (i,j),任何行李被放置在这个单元格应该最终结束在 ( n , m ) (n,m) (n,m).

最初这个可能不成立,但是,你可以,允许一些改变 方向对于某些单元格 使得传送带工作。请确定最小化数值的单元格 你改变的。

请注意,他总是可以使得所有传送带工作 通过改变方向 对于某些单元格。

题意:

给你一个表,每个单元格上有方向R或者D,求每一个点都可以到最后 ( n , m ) (n,m) (n,m)点,更改的最小值。

题解:

因为每个点都是 R D 那么除了最右边和最下面的,每个点都可以跑到最右边或者最下面,所以只要将这两个边变成都会走向 最后一个点就行了。即统计最右边有几个R 最下面有几个D, c n t cnt cnt 就是最小修改值。

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 105;

char s[MAX][MAX];

int main()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        int n, m;
        int ans = 0;

        scanf("%d%d", &n, &m);

        for (int i = 1; i <= n; i++)
            scanf("%s", s[i] + 1);
        
        for (int i = 1; i < n; i++)
        {
            if (s[i][m] == 'R')
                ans++;
        }
        for (int i = 1; i < m; i++)
        {
            if (s[n][i] == 'D')
                ans++;
        }

        printf("%d\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值