Robots on a Grid CodeForces - 1335F(拓扑排序+正反建图+判环)
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
題意:有一個包含n*m個格子的圖形,每一個格子被染成黑色(0)或者白色(1)。每一個格子都有指定的指向,或上或下或左或右。每一個格子最多只能放置一個機器人,所有的機器人一起移動,不允許多個機器人處在同一個格子,時間是無限長的。問最多可以放置多少個機器人。在這一前提下,最多有多少機器人一開始可以放置在黑色的格子里。
思路:因為時間是無限長的,所以若想滿足條件,只能無限循環,這就要求我們去找這個有向圖中的所有的環,求環之前需要拓撲排序一下,將不在環中的點都給處理掉。第一個答案就是所有環的長度加起來了。那么第二個答案怎么弄呢?如圖所示:
假如1的顏色是白色,2的顏色是白色,3的顏色是黑色的話,我們一開始在2,3放置機器人,那么第二個答案就是1,這樣的話是最大的。因此,我們在dfs找環的時候,就把這個環延伸出去的單鏈也處理一下,找出這些單鏈以及環上面的黑色的點。但是我們怎么判斷能不能在這個點上放置機器人呢?如圖所示:
dis數組就是dfs的過程中處理出來的,類似于樹中的深度。這個圖,我們可以看到第二個答案是2,在2或者4或者6,5放置的時候,這樣是最大的,我們可以發現,2和4和6對環的長度2取余都是0,他們不能同時放置機器人,而5對環的長度2取余是1,可以和2,4,6同時放置,也就是dis[i]%len這個數所代表的的點上只能放置一個(len代表環的長度)。上面那個圖怎樣處理出dis數組呢?我們肯定是從環上面的某一點出發,這樣的話,我們反向建圖,然后就可以處理出來了。
總結一下:一開始正反建圖,正向建圖是為了拓撲排序,處理出環;反向建圖是為了從環上某一點出發,處理出dis數組,從而求出答案2.
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Robots on a Grid CodeForces - 1335F(拓扑排序+正反建图+判环)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何从网上办理医保卡?
- 下一篇: [蓝桥杯][历届试题]蚂蚁感冒(模拟)