A ball bounces inside an R by C grid of cells (rows 0..R-1, columns 0..C-1). It starts on a given cell with a diagonal velocity (dr, dc), where each of dr and dc is either -1 or +1. Each step it tries to move by its current velocity. Before committing the move, if moving would carry the row out of [0, R-1] the vertical component dr is reversed, and independently if moving would carry the column out of [0, C-1] the horizontal component dc is reversed; then the ball moves by the (possibly reversed) velocity. Report the cell after exactly T steps.
Line 1: two integers R and C.
Line 2: four integers r0 c0 dr dc (the start cell and the velocity, each of dr, dc in {-1, 1}).
Line 3: an integer T, the number of steps.
A single line r c: the ball's cell after T steps.
Example 1
Input
4 5 0 0 1 1 3
Expected
3 3
Explanation
From (0,0) heading down-right with no wall in range for 3 steps, the ball reaches (3,3).
Example 2
Input
3 3 0 0 1 1 4
Expected
0 0
Explanation
The ball reaches (2,2), bounces off the corner to (1,1), then to (0,0) after 4 steps.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →