CF1807F.Bouncy Ball

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a room that can be represented by a n×mn \times m grid. There is a ball at position (i1,j1)(i_1, j_1) (the intersection of row i1i_1 and column j1j_1 ), and it starts going diagonally in one of the four directions:

  • The ball is going down and right, denoted by DR\texttt{DR} ; it means that after a step, the ball's location goes from (i,j)(i, j) to (i+1,j+1)(i+1, j+1) .
  • The ball is going down and left, denoted by DL\texttt{DL} ; it means that after a step, the ball's location goes from (i,j)(i, j) to (i+1,j1)(i+1, j-1) .
  • The ball is going up and right, denoted by UR\texttt{UR} ; it means that after a step, the ball's location goes from (i,j)(i, j) to (i1,j+1)(i-1, j+1) .
  • The ball is going up and left, denoted by UL\texttt{UL} ; it means that after a step, the ball's location goes from (i,j)(i, j) to (i1,j1)(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)(1, 7) and goes DL\texttt{DL} until it reaches the bottom wall, then it bounces and continues in the direction UL\texttt{UL} . After reaching the left wall, the ball bounces and continues to go in the direction UR\texttt{UR} . When the ball reaches the upper wall, it bounces and continues in the direction DR\texttt{DR} . After reaching the bottom-right corner, it bounces once and continues in direction UL\texttt{UL} , and so on.

Your task is to find how many bounces the ball will go through until it reaches cell (i2,j2)(i_2, j_2) in the room, or report that it never reaches cell (i2,j2)(i_2, j_2) by printing 1-1 .

Note that the ball first goes in a cell and only after that bounces if it needs to.

输入格式

The first line contains a single integer tt ( 1t10001 \leq t \leq 1000 ) — the number of test cases.

The first line of each test case contains six integers and a string n,m,i1,j1,i2,j2,dn, m, i_1, j_1, i_2, j_2, d ( 2n,m250002 \leq n, m \leq 25000 ; 1i1,i2n1 \leq i_1, i_2 \leq n ; 1j1,j2m1 \leq j_1, j_2 \leq m ; d{DR,DL,UR,UL}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 nmn \cdot m over all test cases does not exceed 51045 \cdot 10^4 .

输出格式

For each test case, output a single integer — the number of bounces the ball does until it reaches cell (i2,j2)(i_2, j_2) for the first time, or 1-1 if the ball never reaches the final cell.

输入输出样例

  • 输入#1

    6
    5 7 1 7 2 4 DL
    5 7 1 7 3 2 DL
    3 3 1 3 2 2 UR
    2 4 2 1 2 2 DR
    4 3 1 1 1 3 UL
    6 4 1 2 3 4 DR

    输出#1

    3
    -1
    1
    -1
    4
    0
首页