E. Robot on the Board 1(模拟&思维)

考虑以左上角 ( 1 , 1 ) (1,1) (1,1)为起点,维护四个变量即 x x x的最值和 y y y的最值。

每次把 ( l x , l y ) (lx,ly) (lx,ly) ( 1 , 1 ) (1,1) (1,1)作变换,即: a n s x = 1 − l x , a n s y = 1 − l y ans_x=1-lx,ans_y=1-ly ansx=1lx,ansy=1ly

保证左上角在矩形内。如果此时的 r x − l x ≥ n ∣ ∣ r y − l y > = m rx-lx\ge n||ry-ly>=m rxlxnryly>=m b r e a k break break,即此时不能 再变换了,直接输出结果即可。

// Problem: E. Robot on the Board 1
// Contest: Codeforces - Codeforces Round #753 (Div. 3)
// URL: https://codeforces.ml/contest/1607/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Date: 2021-11-07 14:32:42
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e6+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr) 
void Print(int *a,int n){
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\n",a[n]); 
}
char s[N];
int main(){
	int t;cin>>t;
	while(t--){
		int n,m;cin>>n>>m;
		cin>>(s+1);
		int l=strlen(s+1);
		int u=1,v=1;
		int lx,rx,ly,ry;
		lx=ly=rx=ry=0;
		int x=0,y=0;
		rep(i,1,l){
			if(s[i]=='L') y--;
			else if(s[i]=='R') y++;
			else if(s[i]=='U')x--;
			else x++;
			lx=min(lx,x),rx=max(rx,x);
			ly=min(ly,y),ry=max(ry,y);
			if(rx-lx>=n||ry-ly>=m) break;
			u=1-lx,v=1-ly;
		}
		printf("%d %d\n",u,v);
	}
	return 0;
}