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.
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 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). 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. 有一个n*m的网格,每一个格子都有颜色,每一个格子都有前进的方向,需要在这些格子中放置一些机器人,机器人会按格子的方向走,每一个格子没一次都会走一步,保证所以机器人都不会冲突,多个机器人走到同一个格子,同时要使初始状态机器人所在的黑色格子数尽量多,输出可放置的最多机器人数量,和初始占领的最多黑色格子数 如果两个机器人会冲突说明某一步后所在同一个格子。而最多走n*m步(这个网格中最大的环就是n*m)Output
题目大意
那就算出n*m步后每一个格子的机器人将走到的格子。遍历一遍如果某一个格子n*m步后能被黑色格子走到说明对机器人数和黑色格子数都有贡献,如果某一个格子n*m步后能被白色格子走到说明对机器人数有贡献…累加输出
数组开不了,需要转成一维
数组很大t也很大不能直接用memset置0
用循环置0(n*m多大就置多大…)#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+5;
const int inf=0x3f3f3f3f;
int fa[maxn][22],c[maxn];
char s[maxn];
int bla[maxn],whi[maxn];
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%s",s+1);
for(int j=1;j<=m;++j)c[(i-1)*m+j]=s[j]-'0';
}
for(int i=1;i<=n;++i){
scanf("%s",s+1);
for(int j=1;j<=m;++j){
int to=(i-1)*m+j;
if(s[j]=='L')--to;
else if(s[j]=='R')++to;
else if(s[j]=='U')to-=m;
else to+=m;
fa[(i-1)*m+j][0]=to;
}
}
n*=m;
for(int j=1;j<=20;++j){
for(int i=1;i<=n;++i){
fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
int all=0,ans=0;
for(int i=1;i<=n;++i){
int to=i,y=n;
for(int j=20;j>=0;--j)
if(y&(1<<j))to=fa[to][j];
if(c[i]==0)bla[to]=1;
else whi[to]=1;
}
for(int i=1;i<=n;++i){
if(bla[i]==1)++all,++ans;
else if(whi[i]==1)++all;
}
for(int i=1;i<=n;++i)bla[i]=0,whi[i]=0;
printf("%d %d\n",all,ans);
}
return 0;
}