Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =

75 篇文章 0 订阅

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| ≤ 100s 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).


题目大意:


按照给出的走路顺序无限重复下去,问能否走到点(a,b),起点在(0,0);


思路:


细节很多。

①我们首先模拟多次重复,如果能够走到终点,那么就是Yes.

②否则我们记下模拟走一回合到达的位子xx,yy.如果此时一回合的走向将与目标点相反走向,结果就是No.如果此时xx==0&&yy==0,结果也是No

③除上述两种情况以外,我们再模拟一次走一回合的过程,当我们走到点(x,y)的时候,我们如果有:

x+K*xx==a&&y+K*yy==b,那么结果就是Yes.否则整个过程不能通过循环来得到,结果也是No.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
char a[1500];
int main()
{
    ll A,B;
    while(~scanf("%I64d%I64d",&A,&B))
    {
        scanf("%s",a);
        ll n=strlen(a);
        ll xx,yy;
        xx=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(a[j]=='U')ty++;
                if(a[j]=='D')ty--;
                if(a[j]=='L')tx--;
                if(a[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(a[i]=='U')yy++;
            if(a[i]=='D')yy--;
            if(a[i]=='L')xx--;
            if(a[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(a[i]=='U')y++;
            if(a[i]=='D')y--;
            if(a[i]=='L')x--;
            if(a[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");
    }
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值