Om Nom and Spiders
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
- to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
- to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
3 3 4
...
R.L
R.U
0 2 2
2 2 2
..
RL
1 1
2 2 2
..
LR
0 0
3 4 8
....
RRLL
UUUU
1 3 3 1
2 2 2
..
UU
0 0
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
- If Om Nom starts from the first cell of the first row, he won't see any spiders.
- If he starts from the second cell, he will see two spiders at time 1.
- If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
这道题来自code forces
题目的大概意思就是说Om Nom从第一行的每一列出发,只能往下走,当Om Nom和蜘蛛同一时刻走进同一个洞时,才算遇见了蜘蛛(蜘蛛也会移动)。
要你打出Om Nom在每一列遇见蜘蛛的个数。
简单分析一下:
这个题目就是统计每一列同一时刻到达同一点能和几只蜘蛛碰到
分为四种情况讨论
1.D,这样就碰不到蜘蛛
2.U,往上走的话要和蜘蛛相隔偶数步才能在同一时刻在同一个洞碰见
3.R,假设我蜘蛛起始点是(i,j)蜘蛛往右走,假设Om Nom在x列往下走,那么在他们相遇的地方,Om Nom要往下走i步,推出蜘蛛要往右走i步才能在同一时刻在同一个洞碰见,当前在j,所以就有i+j=x,记住不能越界。
4.L,同R。
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<cmath> 5 #include<algorithm> 6 #include<iostream> 7 using namespace std; 8 char a[2005][2005]; 9 int main() 10 { 11 int n,m,k; 12 int i,j; 13 int num[2005]; 14 memset(num,0,sizeof(num)); 15 memset(a,0,sizeof(a)); 16 scanf("%d %d %d",&n,&m,&k); 17 for(i=0;i<n;i++) 18 scanf("%s",a[i]); 19 for(i=1;i<n;i++) 20 { 21 for(j=0;j<m;j++) 22 { 23 if(a[i][j]=='R'&&i+j<m) //i+j<m表示不能出界 24 num[i+j]++; 25 if(a[i][j]=='U'&&i%2==0) 26 num[j]++; 27 if(a[i][j]=='L'&&j-i>=0) //j-i>=0表示不能出界 28 num[j-i]++; 29 } 30 } 31 for(i=0;i<m;i++) 32 printf("%d ",num[i]); 33 printf("\n"); 34 return 0;