Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Created March 19, 2023 17:12
Show Gist options
  • Save qjatn0120/198671d305cb50a4e0d059cbaded2c2f to your computer and use it in GitHub Desktop.
Save qjatn0120/198671d305cb50a4e0d059cbaded2c2f to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
void nxt(int n, int m, int &r, int &c, int &d, int &bounce){
if(d & 1) c--;
else c++;
if(d & 2) r--;
else r++;
int flag = 0;
if(r == 0) r = 2, d ^= 2, flag = 1;
if(r == n + 1) r = n - 1, d ^= 2, flag = 1;
if(c == 0) c = 2, d ^= 1, flag = 1;
if(c == m + 1) c = m - 1, d ^= 1, flag = 1;
bounce += flag;
}
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int n, m, r1, r2, c1, c2, d;
string s;
cin >> n >> m >> r1 >> c1 >> r2 >> c2 >> s;
if(s == "DR") d = 0;
else if(s == "DL") d = 1;
else if(s == "UR") d = 2;
else d = 3;
vector <vector <vector <bool> > > visited(n + 1, vector <vector <bool> >(m + 1, vector <bool> (4)));
int ans = 0;
while(r1 != r2 || c1 != c2){
if(visited[r1][c1][d]){
ans = -1;
break;
}
visited[r1][c1][d] = true;
nxt(n, m, r1, c1, d, ans);
}
cout << ans << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment