1 second
256 megabytes
standard input
standard output
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).
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.
Print "Yes" if the robot will be located at (a, b), and "No" otherwise.
细节蛮多的,做的我好忧伤。。。
#include <iostream>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; int main()
{
char s[];
int a, b, dx, dy, i;
while(scanf("%d %d", &a, &b) != EOF)
{
scanf("%s", s);
dx = dy = ;
for(i = ; s[i] != '\0'; i++)
{
if(dx == a && dy == b) break;
if(s[i] == 'U') dy++;
else if(s[i] == 'D') dy--;
else if(s[i] == 'L') dx--;
else dx++;
}
if(s[i] == '\0')
{
int dx2 = abs(dx), dy2 = abs(dy);
for(i = ; s[i] != '\0'; i++)
{
if(s[i] == 'U') b--;
else if(s[i] == 'D') b++;
else if(s[i] == 'L') a++;
else a--;
if(!a && !b) break;
int a2 = abs(a), b2 = abs(b);
if((long long)a * dy == (long long)b * dx && (long long)b * dy >= && (long long)a * dx >= )
{
if(dy && dx)
{
if(a2 % dx2 == && b2 % dy2 == ) break;
}
else if(!dx && dy)
{
if(!a && b2 % dy2 == ) break;
}
else if(dx && !dy)
{
if(!b && a2 % dx2 == ) break;
}
}
}
}
if(s[i] != '\0') puts("Yes");
else puts("No");
}
return ;
}