You are given a room that can be represented by a 𝑛×𝑚n×m grid. There is a ball at position (𝑖1,𝑗1)(i1,j1) (the intersection of row 𝑖1i1 and column 𝑗1j1), and it starts going diagonally in one of the four directions:原题链接
- The ball is going down and right, denoted by 𝙳𝚁DR; it means that after a step, the ball's location goes from (𝑖,𝑗)(i,j) to (𝑖+1,𝑗+1)(i+1,j+1).
- The ball is going down and left, denoted by 𝙳𝙻DL; it means that after a step, the ball's location goes from (𝑖,𝑗)(i,j) to (𝑖+1,𝑗−1)(i+1,j−1).
- The ball is going up and right, denoted by 𝚄𝚁UR; it means that after a step, the ball's location goes from (𝑖,𝑗)(i,j) to (𝑖−1,𝑗+1)(i−1,j+1).
- The ball is going up and left, denoted by 𝚄𝙻UL; it means that after a step, the ball's location goes from (𝑖,𝑗)(i,j) to (𝑖−1,𝑗−1)(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 until it reaches the bottom wall, then it bounces and continues in the direction 𝚄𝙻UL. After reaching the left wall, the ball bounces and continues to go in the direction 𝚄𝚁UR. When the ball reaches the upper wall, it bounces and continues in the direction 𝙳𝚁DR. After reaching the bottom-right corner, it bounces once and continues in direction 𝚄𝙻UL, and so on.
Your task is to find how many bounces the ball will go through until it reaches cell (𝑖2,𝑗2)(i2,j2) in the room, or report that it never reaches cell (𝑖2,𝑗2)(i2,j2) by printing −1−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≤𝑡≤10001≤t≤1000) — the number of test cases.
The first line of each test case contains six integers and a string 𝑛,𝑚,𝑖1,𝑗1,𝑖2,𝑗2,𝑑n,m,i1,j1,i2,j2,d (2≤𝑛,𝑚≤250002≤n,m≤25000; 1≤𝑖1,𝑖2≤𝑛1≤i1,i2≤n; 1≤𝑗1,𝑗2≤𝑚1≤j1,j2≤m; 𝑑∈{𝙳𝚁,𝙳𝙻,𝚄𝚁,𝚄𝙻}d∈{DR,DL,UR,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⋅m over all test cases does not exceed 5⋅1045⋅104.
Output
For each test case, output a single integer — the number of bounces the ball does until it reaches cell (𝑖2,𝑗2)(i2,j2) for the first time, or −1−1 if the ball never reaches the final cell.
Example
input
Copy
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
output
Copy
3 -1 1 -1 4 0
解析:其实题目怎么说的就按照他说的去做就可以了;
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<cstring>
#include <unordered_set>
#include<queue>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
#define rep(a,b) for(ll i=a;i<=b;i++)
#define rrep(a,b) for(ll j=a;j<=b;j++)
#define per(a,b) for(ll i=a;i>=b;i--)
#define pper(a,b) for(ll j=a;j>=b;j--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
const int N = 2e5 + 100;
const int INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
// ll h[N],ne[N],w[N],to[N],idx;
// void add(int a,int b,int c)
// {
// to[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
// }
const ll mod =998244353;
ll t, n, m, a, b, c,d, x,y, k, cnt, ans, ant, sum,q, p,idx;
ll arr[N], brr[N], crr[N];
char mp[5][5];
int main()
{
cin>>t;
while(t--)
{
int x1,x2,y1,y2;
cin>>n>>m>>x1>>y1>>x2>>y2;
string s;
ans=0;
cin>>s;
if(x1==x2 && y1==y2)//如果开始地点和终止地点相同则输出
{
cout<<0<<endl;
continue;
}
int dx,dy;//移动方向
if(s[0]=='U')
{
dx=-1;
}else{
dx=1;
}
if(s[1]=='L')
{
dy=-1;
}else{
dy=1;
}
x=x1,y=y1;
cnt=0;
while(x2!=x || y2!=y){
int flag=0;
if(x+dx > n || x+dx <= 0) //如果出界了,那么就反弹
{
dx = -dx;
ans++ ;
flag++;
}
if(y+dy > m || y+dy <= 0)//如果出界了,那么就反弹
{
dy = -dy;
ans++;
flag++;
}
ans -= (flag == 2);//如果当x和y都出界的话,其实是算一次出界的
x += dx;
y += dy;//表示当前位置
if(ans > n * m){//把所有边都撞完了,还没到点,那就是不存在答案了
cnt = 1;
break;
}
}
if(cnt==1)
{
cout<<-1<<endl;
}else{
cout<<ans<<endl;
}
}
}