Stroll
Time limit2sMemory limit128 MB
Simulate the letters on a grid as N successive walks from the top-left, and report the endpoint of the N-th walk.
- Level
Medium5 of 10
- Topics
- Simulation, Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
Sanggeun goes for a stroll every day to stay healthy.
His town is laid out like a go board, with horizontal roads and vertical roads. Call each point where two roads meet an intersection. The intersection in the -th row from the top and the -th column from the left is written . Sanggeun's house sits at the top-left intersection , and every stroll starts there.
Each of the intersections from to has a single direction letter written on it: R means right and D means down.
One stroll proceeds by the following rule. If the letter on the current intersection is
- R, change it to D and move to the intersection to the right;
- D, change it to R and move to the intersection below.
He repeats this until he reaches an intersection on the rightmost vertical road (column ) or the bottommost horizontal road (row ), where the stroll ends. These boundary intersections have no letter.
Because the letters change as he walks, each stroll can follow a different route. Sanggeun wonders where his -th stroll will end if he keeps repeating this process.
Given , , and the letter initially written on each intersection, write a program that finds the intersection where the -th stroll ends.
Input
The first line contains three integers , , and , separated by spaces. (, )
Each of the next lines contains integers. The -th integer on the -th line describes the letter initially written on intersection : is D (down) and is R (right).
Output
Let be the intersection where the -th stroll ends. Print and on one line, separated by a space.