链接.
思路:记录每次执行操作后的LRDU的最大值,当无论初始在哪都会撞墙时就结束。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t,n,m;
char s[1000010];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
memset(s,0,sizeof s);
cin>>n>>m>>s;
int len=strlen(s);
int l=0,r=0,u=0,d=0;
int x=0,y=0;
int ax=-1,ay=-1;
for(int i=0;i<len;i++)
{
if(s[i]=='R')x++;
if(s[i]=='L')x--;
if(s[i]=='U')y++;
if(s[i]=='D')y--;
if(x>0)r=max(r,x);
else l=max(l,-x);
if(y>0)u=max(u,y);
else d=max(d,-y);
//cout<<u<<" "<<d<<" "<<l<<" "<<r<<"\n";
if(l+r<m&&u+d<n&&i<len-1);
else
{
if(l+r==m)
{
if(s[i]=='L')ax=m-r;
else ax=l+1;
ay=u+1;
}
else if(u+d==n)
{
if(s[i]=='U')ay=n-d;
else ay=u+1;
ax=l+1;
}
else
{
ax=l+1;
ay=u+1;
}
break;
}
}
cout<<ay<<" "<<ax<<"\n";
}
return 0;
}