CodeForces 321A


A. 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 strings. 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, (-109a,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.

Sample test(s)
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).



官方题解:

322C - Ciel and Robot 321A - Ciel and Robot

Note that after Ciel execute string s, it will moves (dx, dy).

And for each repeat, it will alway moves (dx, dy).

So the total movement will be k * (dx, dy) + (dx[p], dy[p]) which (dx[p], dy[p]) denotes the movement after execute first p characters.

We can enumerate p since (0 <= p < |s| <= 100), and check if there are such k exists.

Note that there are some tricks:

We can divide dx or dy directly because they both can become zero.

Another trick is that k must be non-negative.

Many people failed on this test case (which no included in the pretest):

-1 -1

UR



#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

int dx[111],dy[111];
int ta,tb;
char str[111];

int main()
{
    cin>>ta>>tb;
    cin>>str;

    memset(dx,0,sizeof(dx));
    memset(dy,0,sizeof(dy));

    int len=strlen(str);

    for(int i=0;i<len;i++)
    {
        if(str=='U')
        {
            dy[i+1]=dy+1;
            dx[i+1]=dx;
        }
        else if(str=='D')
        {
            dy[i+1]=dy-1;
            dx[i+1]=dx;
        }
        else if(str=='L')
        {
            dx[i+1]=dx-1;
            dy[i+1]=dy;
        }
        else if(str=='R')
        {
            dx[i+1]=dx+1;
            dy[i+1]=dy;
        }
    }
/*
    for(int i=0;i<=len;i++)
    {
        cout<<dx<<" and "<<dy<<endl;
    }
*/
    int ddx=dx[len],ddy=dy[len];

    int OK=0;
    if(ta==0&&tb==0) OK=1;

    if(!OK)
    for(int i=1;i<=len;i++)
    {
        if(ta==dx&&tb==dy)
        {
            OK=1;
            break;
        }
    }

    if(!OK)
    for(int i=0;i<len;i++)
    {
        int kx=ta-dx;
        int ky=tb-dy;

        if(ddx==0&&ddy==0)
        {
            if(kx==0&&ky==0)
            {
                OK=1;break;
            }
        }
        else if(ddx==0&&ddy!=0)
        {
            if(kx==0)
            {
                if(ky%ddy==0)
                {
                    if(ky/ddy>=0)
                    {
                        OK=1;
                        break;
                    }
                }
            }
        }
        else if(ddy==0&&ddx!=0)
        {
            if(ky==0)
            {
                if(kx%ddx==0)
                {
                    if(kx/ddx>=0)
                    {
                        OK=1;
                        break;
                    }
                }
            }
        }
        else if(ddx!=0&&ddy!=0)
        {
            if(kx%ddx==0&&ky%ddy==0)
            {
                int t1=kx/ddx;
                int t2=ky/ddy;
                if(t1==t2)
                {
                    if(t1>=0)
                    {
                        OK=1;
                        break;
                    }
                }
            }
        }

    }

    if(OK==1)
    {
        puts("Yes");
    }
    else puts("No");

    return 0;
}

posted @ 2013-07-02 08:17  码代码的猿猿  阅读(277)  评论(0编辑  收藏  举报