(倍增)CodeForces - 1335F Robots on a Grid

48 篇文章 0 订阅
1 篇文章 0 订阅

思路:可以先视为在每个格子都放了一个机器人,在让他们同时走至少n*m步,这样,就只要在最后每个格子检查一下是否有机器人,以及机器人是否来自黑格,当然,这个过程不能直接模拟,我们要进行倍增

nex[j[i]表示第j号格子走2^(i-1)步到达的格子,第j个位置走2^(i-1)步可以看成第j个点走2^(i-2)到nex[j][i-1]再走2^(i-2)步,这就完成了倍增

 There is a rectangular grid of size n×m. Each cell of the grid is colored black ('0') or white ('1'). The color of the cell (i,j) is ci,j. You are also given a map of directions: for each cell, there is a direction si,j

which is one of the four characters 'U', 'R', 'D' and 'L'.

  • If si,j

is 'U' then there is a transition from the cell (i,j) to the cell (i−1,j)

  • ;
  • if si,j
  • is 'R' then there is a transition from the cell (i,j) to the cell (i,j+1)
  • ;
  • if si,j
  • is 'D' then there is a transition from the cell (i,j) to the cell (i+1,j)
  • ;
  • if si,j
  • is 'L' then there is a transition from the cell (i,j) to the cell (i,j−1)
    • .

    It is guaranteed that the top row doesn't contain characters 'U', the bottom row doesn't contain characters 'D', the leftmost column doesn't contain characters 'L' and the rightmost column doesn't contain characters 'R'.

    You want to place some robots in this field (at most one robot in a cell). The following conditions should be satisfied.

    • Firstly, each robot should move every time (i.e. it cannot skip the move). During one move each robot goes to the adjacent cell depending on the current direction.
    • Secondly, you have to place robots in such a way that there is no move before which two different robots occupy the same cell (it also means that you cannot place two robots in the same cell). I.e. if the grid is "RL" (one row, two columns, colors does not matter there) then you can place two robots in cells (1,1)
    and (1,2), but if the grid is "RLL" then you cannot place robots in cells (1,1) and (1,3) because during the first second both robots will occupy the cell (1,2)
    • .

    The robots make an infinite number of moves.

    Your task is to place the maximum number of robots to satisfy all the conditions described above and among all such ways, you have to choose one where the number of black cells occupied by robots before all movements is the maximum possible. Note that you can place robots only before all movements.

    You have to answer t

    independent test cases.

    Input

    The first line of the input contains one integer t

    (1≤t≤5⋅104) — the number of test cases. Then t

    test cases follow.

    The first line of the test case contains two integers n

    and m (1<nm≤106

    ) — the number of rows and the number of columns correspondingly.

    The next n

    lines contain m characters each, where the j-th character of the i-th line is ci,j (ci,j is either '0' if the cell (i,j) is black or '1' if the cell (i,j)

    is white).

    The next n

    lines also contain m characters each, where the j-th character of the i-th line is si,j (si,j is 'U', 'R', 'D' or 'L' and describes the direction of the cell (i,j)

    ).

    It is guaranteed that the sum of the sizes of fields does not exceed 106

    (∑nm≤106

    ).

    Output

    For each test case, print two integers — the maximum number of robots you can place to satisfy all the conditions described in the problem statement and the maximum number of black cells occupied by robots before all movements if the number of robots placed is maximized. Note that you can place robots only before all movements.

    Example

    Input

    3
    1 2
    01
    RL
    3 3
    001
    101
    110
    RLL
    DLD
    ULL
    3 3
    000
    000
    000
    RRD
    RLD
    ULL
    

    Output

    2 1
    4 3
    2 2
    

    Sponsor

#include <cstdio>
#include<iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include<set>
#include<vector>
using namespace std;
#define ll long long
//#define ll int
//#define maxn 1050
#define maxm 1050000
string a[maxm],b[maxm];
int nex[maxm][25];int n,m,ans1[maxm],ans2[maxm];
int js(int x,int y)
{
    return (x-1)*m+y;
}
int main()
{
     /*ios::sync_with_stdio(false);
    cin.tie(0);
     cout.tie(0);*/
     ios::sync_with_stdio(0);cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            a[i]=' '+a[i];
            //cout<<a[i]<<'\n';

        }
        for(int i=1;i<=n;i++)
        {
            cin>>b[i];
            b[i]=' '+b[i];
            //cout<<b[i]<<'\n';
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {

                int x=js(i,j);
                ans1[x]=ans2[x]=0;
                if(b[i][j]=='U') nex[x][0]=js(i-1,j);
                if(b[i][j]=='D') nex[x][0]=js(i+1,j);
                if(b[i][j]=='L') nex[x][0]=js(i,j-1);
                if(b[i][j]=='R') nex[x][0]=js(i,j+1);
            }
        }
        for(int i=1;i<=22;i++)
        {
            for(int j=1;j<=n*m;j++)
            {
                nex[j][i]=nex[nex[j][i-1]][i-1];//第j个位置走2^(i-1)步可以看成第j个点走2^(i-2)到nex[j][i-1]再走2^(i-2)步,这就完成了倍增

            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                int x=js(i,j);
                if(a[i][j]=='1') ans1[nex[x][22]]++;//每个位置有多少机器人
                if(a[i][j]=='0')
                {
                    ans1[nex[x][22]]++;
                    ans2[nex[x][22]]++;//每个位置有多少来自黑格的机器人
                }
            }
        }
        int x=0,y=0;
        for(int i=1;i<=n*m;i++)
        {
            x+=(ans1[i]!=0);
            y+=(ans2[i]!=0);
        }

        cout<<x<<" "<<y<<'\n';
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值