CodeForces 321A Ciel and Robot(模拟)

链接:http://codeforces.com/problemset/problem/321/A

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

Input
2 2
RU
Output
Yes
Input
1 2
RU
Output
No
Input
-1 1000000000
LRRLU
Output
Yes
Input
0 0
D
Output
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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值