您的位置:首页 > 其它

Ciel and Robot

2013-08-07 01:12 260 查看
C. Ciel and Robot

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
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).

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.

细节蛮多的,做的我好忧伤。。。

#include <iostream>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

int main()
{
char s[101];
int a, b, dx, dy, i;
while(scanf("%d %d", &a, &b) != EOF)
{
scanf("%s", s);
dx = dy = 0;
for(i = 0; 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 = 0; 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 >= 0 && (long long)a * dx >= 0)
{
if(dy && dx)
{
if(a2 % dx2 == 0 && b2 % dy2 == 0)  break;
}
else if(!dx && dy)
{
if(!a && b2 % dy2 == 0)  break;
}
else if(dx && !dy)
{
if(!b && a2 % dx2 == 0) break;
}
}
}
}
if(s[i] != '\0') puts("Yes");
else puts("No");
}
return 0;
}


View Code

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: