:loop
rand.exe
my.exe
std.exe
fc myout.txt stdout.txt
if not errorlevel 1 goto loop
pause
goto loop
#include
#define int long long
#define rep(i,j,k) for(int i=(j),_i=(k);i=_i;--i)
using namespace std;
inline int read(){
int x=0,f=0;char ch;
while(!isdigit(ch=getchar())) f|=ch=='-';
while(isdigit(ch)) x=(x<<3)+(x<>s;
if(s=="UL") pos=1;
if(s=="DR") pos=2;
if(s=="DL") pos=3;
if(s=="UR") pos=4;
int cnt=0;
while(x1!=x2||y1!=y2) {
if(vis[num(x1,y1)][pos]==2) {ans=-1;break;}
vis[num(x1,y1)][pos]++;
if(cnt) {x1+=xx[pos];y1+=yy[pos];}
cnt=1;
if(x1==x2&&y1==y2) break;
if(x1==1&&y1==1) {ans+=(pos!=2);pos=2;continue;}
if(x1==1&&y1==m) {ans+=(pos!=3);pos=3;continue;}
if(x1==n&&y1==1) {ans+=(pos!=4);pos=4;continue;}
if(x1==n&&y1==m) {ans+=(pos!=1);pos=1;continue;}
if(x1==1) {
ans+=(pos==1||pos==4);
if(pos==1) pos=3;
if(pos==4) pos=2;
continue;
}
if(x1==n) {
ans+=(pos==2||pos==3);
if(pos==2) pos=4;
if(pos==3) pos=1;
continue;
}
if(y1==1) {
ans+=(pos==1||pos==3);
if(pos==1) pos=4;
if(pos==3) pos=2;
continue;
}
if(y1==m) {
ans+=(pos==2||pos==4);
if(pos==2) pos=3;
if(pos==4) pos=1;
continue;
}
}
printf("%lld\n",ans);
memset(vis,0,sizeof(vis));
}
}