Description
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all:
- 'U': go up, (x, y) → (x, y+1);
- 'D': go down, (x, y) → (x, y-1);
- 'L': go left, (x, y) → (x-1, y);
- 'R': go right, (x, y) → (x+1, y).
The robot will do the operations in s from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (a, b).
Input
The first line contains two integers a and b, ( - 109 ≤ a, b ≤ 109). The second line contains a string s (1 ≤ |s| ≤ 100, s only contains characters 'U', 'D', 'L', 'R') — the command.
Output
Print "Yes" if the robot will be located at (a, b), and "No" otherwise.
Sample Input
2 2 RU
Yes
1 2 RU
No
-1 1000000000 LRRLU
Yes
0 0 D
Yes
Sample Output
Hint
In the first and second test case, command string is "RU", so the robot will go right, then go up, then right, and then up and so on.
The locations of its moves are (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → ...
So it can reach (2, 2) but not (1, 2).
题意:'U','D','L','R'分别代表向上、向下,向左,向右,走一步,字符串可以无限循环下去,问是否能够经过(a,b)这个点
解题:第一步,先将字符串模拟一遍记录下经过的所有位置x[i],y[i],最后到达tx,ty位置,那么枚举每对(x[i],y[i]),假设再经过K轮循环就能到达目的地(a,b),那么有x[i]+k*tx=a, y[i]+k*ty=b,如果假设成立,那么K有非负解。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=105;
LL a,b,x[maxn],y[maxn],tx,ty;
char op[maxn];
bool panduan(LL x,LL y)
{
LL dex=a-x,dey=b-y;
if(tx==0)
{
if(ty==0)
{
if(dex==0 && dey==0) return true;
else return false;
}
else
{
if(dey%ty==0){
if(dey/ty>=0 && dex==0)
return true;
else
return false;
}
else
return false;
}
}
else
{
if(ty==0)
{
if(dex%tx==0)
if(dex/tx>=0 && b==y)
return true;
else
return false;
}
else
{
if(dex%tx==0 && dey%ty==0){
if(dex/tx>=0 && dey/ty>=0 && dex/tx==dey/ty)
return true;
else
return false;
}
else
return false;
}
}
}
int main()
{
while(cin>>a>>b)
{
scanf("%s",op+1);
if(a==0 && b==0)
{
puts("Yes");
continue;
}
int flag=0;
int len=strlen(op+1);
LL X=0,Y=0;
for(int i=1;i<=len;i++)
{
if(op[i]=='U') Y++;
else if(op[i]=='D') Y--;
else if(op[i]=='R') X++;
else if(op[i]=='L') X--;
x[i]=X;
y[i]=Y;
}
tx=x[len];
ty=y[len];
for(int i=1;i<=len;i++)
{
if(panduan(x[i],y[i]))
{
flag=1;
puts("Yes");
break;
}
}
if(!flag)
puts("No");
}
return 0;
}