题目:
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.
Examples
Input
2 2 RU
Output
Yes
Input
1 2 RU
Output
No
Input
-1 1000000000 LRRLU
Output
Yes
Input
0 0 D
Output
Yes
Note
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).
解题报告:比较水的一道题目,难点就是不断的循环寻找每次遍历会达到的点,进行判断。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn =1500;
int main()
{
ll A,B;
char str[maxn];
while(scanf("%I64d%I64d",&A,&B)!=EOF)
{
scanf("%s",str);
ll n=strlen(str);
ll xx,yy;
xx=0;yy=0;
int flag=0;
int tx=0,ty=0;
for(int i=0;i<100000;i++)
{
for(int j=0;j<n;j++)
{
if(tx==A&&ty==B) flag=1;
if(str[j]=='U') ty++;
if(str[j]=='D') ty--;
if(str[j]=='L') tx--;
if(str[j]=='R') tx++;
if(tx==A&&ty==B) flag=1;
}
}
for(ll i=0;i<n;i++)
{
if(xx==A&&yy==B) flag=1;
if(str[i]=='U') yy++;
if(str[i]=='D') yy--;
if(str[i]=='L') xx--;
if(str[i]=='R') xx++;
if(xx==A&&yy==B) flag=1;
}
if(flag==1)
{
printf("Yes\n");
continue;
}
if(xx==yy&&xx==0)
{
printf("No\n");
continue;
}
if(A*xx<0||B*yy<0)
{
printf("No\n");
continue;
}
ll x=0,y=0;
for(ll i=0;i<n;i++)
{
if((A-x)*yy==(B-y)*xx)
{
if(xx!=0&&(A-x)%xx==0)
{
flag=1;
}
if(yy!=0&&(B-y)%yy==0)
{
flag=1;
}
}
if(str[i]=='U') y++;
if(str[i]=='D') y--;
if(str[i]=='L') x--;
if(str[i]=='R') x++;
if((A-x)*yy==(B-y)*xx)
{
if(xx!=0&&(A-x)%xx==0)
{
flag=1;
}
if(yy!=0&&(B-y)%yy==0)
{
flag=1;
}
}
}
if(flag==1) printf("Yes\n");
else printf("No\n");
}
}