现在的位置: 首页 > 综合 > 正文

codeforces A. Ciel and Robot

2013年12月07日 ⁄ 综合 ⁄ 共 2293字 ⁄ 字号 评论关闭
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, ( - 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.

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
这道题的思路很简单,大家都可以想到,但是我写的程序bug太多了,昨晚没调出来,今天在测试数据下还调了几乎一上午,不得不说自己考虑什么真的很不全面,其实都是对除0,以及正负号的,自己的思路很不严密啊,下面贴出修改了n次的代码,o(╯□╰)o!
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<climits>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define repf(i,n,m) for(int i=(n); i<=(m); ++i)
#define repd(i,n,m) for(int i=(n); i>=(m); --i)
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define fab(a) ((a)>0?(a):(0-(a)))
#define ll long long
#define arc(a) ((a)*(a))
#define inf 100000
#define exp 0.000001
#define N 200
char s[N];
ll x,y;
struct node{
	ll x,y;
}a[N];
int main()
{ 
	int sign=0;
   while(cin>>x>>y)
	{
		sign++; 
		cin>>s;
        a[0].x=a[0].y=0;
		int len=strlen(s);
		int flag=0;
        rep(i,len)
		{
			a[i+1].x=a[i].x;a[i+1].y=a[i].y;
           if(s[i]=='U')  a[i+1].y++;
		   if(s[i]=='D')  a[i+1].y--;
		   if(s[i]=='L')  a[i+1].x--;
		   if(s[i]=='R')  a[i+1].x++;
		   if(a[i+1].x==x && a[i+1].y==y)
			   flag=1;
		} 
		if(x==0 && y==0)
		{printf("Yes\n"); continue;}
		ll xx=a[len].x,yy=a[len].y;
		if((xx==0 && yy==0)&& flag==0)
		{printf("No\n");continue;}  
        if(flag==0)
        { 
		  rep(i,len+1)
		  {  
             if(xx==0 && yy!=0 && (y-a[i].y)%yy==0&& a[i].x==x)
               flag=1;
             if(yy==0 && xx!=0 &&(x-a[i].x)%xx==0 && y==a[i].y)
              flag=1;
             if(xx*yy!=0 && (x-a[i].x)%xx==0 &&(y-a[i].y)%yy==0)
              flag=1;
             if(flag==1)
             {   
                 if(xx*yy!=0 && (x-a[i].x)/xx!=(y-a[i].y)/yy)
                  flag=0;
                 if(xx!=0 && (x-a[i].x)*xx<0)
                 flag=0;
                 if(yy!=0 && (y-a[i].y)*yy<0)
                 flag=0;
                 if(flag==1)
                   break;
             }
		  }
        }
		if(flag==1)
			printf("Yes\n");
		else
			printf("No\n");

	 }
   return 0;
}

抱歉!评论已关闭.