Codeforces1391 B. 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 (𝑖,𝑗+1). Similarly, if the cell (𝑖,𝑗) is assigned direction D, any luggage kept on that will move to the cell (𝑖+1,𝑗). 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 𝑡 (1≤𝑡≤10). Description of the test cases follows.

The first line of each test case contains two integers 𝑛,𝑚 (1≤𝑛≤100, 1≤𝑚≤100) — 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.

Example
inputCopy
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
outputCopy
1
3
9
0
Note
In the first case, just changing the direction of (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), it first moves to (3,2) and then to (3,3).

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

题意:
从起点出发去终点。
R代表向右走,D代表向下走。
要求改变最少的格子使得所有点出发都能到达终点。

思路:
一开始看错题了以为是只从起点出发,一直思考为啥DP过不了样例。
然后发现是所有点为起点。那就好办了,把右边界为R的和下边界为D的改一下就好了。

#pragma GCC optimize(2)

#include<iostream>
#include<cstdio>
#include <map>
#include<cstring>
#include <map>
#include <math.h>
#include<queue>
#include <algorithm>

using namespace std;
const int mod = 998244353;
const int maxn = 1e5 + 7;
typedef long long ll;

char s[105][105];

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        int n,m;scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++) {
            scanf("%s",s[i] + 1);
        }
    
        int ans = 0;
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                if(i == n && s[i][j] == 'D') {
                    ans++;
                }
                if(j == m && s[i][j] == 'R') {
                    ans++;
                }
            }
        }

        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值