杂题——Bouncy Ball
Bouncy Ball
You are given a room that can be represented by a $n \times m$ grid. There is a ball at position $(i_1, j_1)$ (the intersection of row $i_1$ and column $j_1$), and it starts going diagonally in one of the four directions:
- The ball is going down and right, denoted by $\texttt{DR}$; it means that after a step, the ball’s location goes from $(i, j)$ to $(i+1, j+1)$.
- The ball is going down and left, denoted by $\texttt{DL}$; it means that after a step, the ball’s location goes from $(i, j)$ to $(i+1, j-1)$.
- The ball is going up and right, denoted by $\texttt{UR}$; it means that after a step, the ball’s location goes from $(i, j)$ to $(i-1, j+1)$.
- The ball is going up and left, denoted by $\texttt{UL}$; it means that after a step, the ball’s location goes from $(i, j)$ to $(i-1, j-1)$.
After each step, the ball maintains its direction unless it hits a wall (that is, the direction takes it out of the room’s bounds in the next step). In this case, the ball’s direction gets flipped along the axis of the wall; if the ball hits a corner, both directions get flipped. Any instance of this is called a bounce. The ball never stops moving.
In the above example, the ball starts at $(1, 7)$ and goes $\texttt{DL}$ until it reaches the bottom wall, then it bounces and continues in the direction $\texttt{UL}$. After reaching the left wall, the ball bounces and continues to go in the direction $\texttt{UR}$. When the ball reaches the upper wall, it bounces and continues in the direction $\texttt{DR}$. After reaching the bottom-right corner, it bounces once and continues in direction $\texttt{UL}$, and so on.
Your task is to find how many bounces the ball will go through until it reaches cell $(i_2, j_2)$ in the room, or report that it never reaches cell $(i_2, j_2)$ by printing $-1$.
Note that the ball first goes in a cell and only after that bounces if it needs to.
Input
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains six integers and a string $n, m, i_1, j_1, i_2, j_2, d$ ($2 \leq n, m \leq 25000$; $1 \leq i_1, i_2 \leq n$; $1 \leq j_1, j_2 \leq m$; $d \in{ \texttt{DR}, \texttt{DL}, \texttt{UR}, \texttt{UL}}$) — the dimensions of the grid, the starting coordinates of the ball, the coordinates of the final cell and the starting direction of the ball.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $5 \cdot 10^4$.
Output
For each test case, output a single integer — the number of bounces the ball does until it reaches cell $(i_2, j_2)$ for the first time, or $-1$ if the ball never reaches the final cell.
Example
input
1 |
|
output
1 |
|
翻译
您将获得一个可以用 $n \times m$ 网格表示的房间。在位置 $(i_1, j_1)$ (行 $i_1$ 和列 $j_1$ 的交集)处有一个球,它开始沿四个方向之一沿对角线移动:
- 球向右下方移动,记为 $\texttt{DR}$ ;这意味着一步之后,球的位置从 $(i, j)$ 变为 $(i+1, j+1)$ 。
- 球向左下方移动,记为 $\texttt{DL}$ ;这意味着一步之后,球的位置从 $(i, j)$ 变为 $(i+1, j-1)$ 。
- 球向右上方移动,记为 $\texttt{UR}$ ;这意味着一步之后,球的位置从 $(i, j)$ 变为 $(i-1, j+1)$ 。
- 球向上并向左移动,记为 $\texttt{UL}$ ;这意味着一步之后,球的位置从 $(i, j)$ 变为 $(i-1, j-1)$ 。
每一步之后,球都会保持其方向,除非它撞到墙壁(也就是说,该方向在下一步中将其带出房间的边界)。在这种情况下,球的方向沿着墙的轴线翻转;如果球撞到角落,两个方向都会翻转。任何这种情况都称为反弹。球永远不会停止移动。
在上面的示例中,球从 $(1, 7)$ 开始,行进 $\texttt{DL}$ 直到到达底墙,然后弹跳并继续朝 $\texttt{UL}$ 方向运动。到达左侧墙壁后,球弹起并继续朝 $\texttt{UR}$ 方向移动。当球到达上墙时,它会弹起并继续朝 $\texttt{DR}$ 方向移动。到达右下角后,它会弹一次并继续朝 $\texttt{UL}$ 方向移动,依此类推。
您的任务是找出球在到达房间中的单元格 $(i_2, j_2)$ 之前将经历多少次弹跳,或者通过打印 $-1$ 报告它从未到达单元格 $(i_2, j_2)$ 。
请注意,球首先进入一个单元格,然后只有在需要时才会反弹。
输入
第一行包含一个整数 $t$ ( $1 \leq t \leq 1000$ ) - 测试用例的数量。
每个测试用例的第一行包含六个整数和一个字符串 $n, m, i_1, j_1, i_2, j_2, d$ ( $2 \leq n, m \leq 25000$ ; $1 \leq i_1, i_2 \leq n$ ; $1 \leq j_1, j_2 \leq m$ ; $d \in{ \texttt{DR}, \texttt{DL}, \texttt{UR}, \texttt{UL}}$ ) - 网格的尺寸,球的起始坐标,最后一个单元格和球的起始方向。
保证所有测试用例的 $n \cdot m$ 之和不超过 $5 \cdot 10^4$ 。
输出
对于每个测试用例,输出一个整数 - 球第一次到达单元格 $(i_2, j_2)$ 之前的弹跳次数,如果球从未到达最后一个单元格,则输出 $-1$ 。
Example
input
1 |
|
output
1 |
|
思路
模拟这个点走的过程,如果走到一个之前走到的状态就说明陷入了循环,此时就是无解,状态可以用坐标和方向来表示,坐标 = (横坐标 - 1) * 列数 + 列坐标.
代码
1 |
|