CodeForces 321A Ciel and Robot(模拟)
来源:互联网 发布:51单片机 编辑:程序博客网 时间:2024/05/16 04:34
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 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 andb, ( - 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
2 2RU
Yes
1 2RU
No
-1 1000000000LRRLU
Yes
0 0D
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;}
- CodeForces 321A Ciel and Robot(模拟)
- CodeForces 321A Ciel and Robot[暴力]
- CodeForces 321 A - Ciel and Robot
- CodeForces 321A Ciel and Robot
- CodeForces 321A Ciel and Robot(数学模拟)
- CodeForces 321A Ciel and Robot(数学模拟 枚举答案)
- codeforces A. Ciel and Robot
- Codeforces 321A Ciel and Robot 枚举答案
- Codeforces Round 190 div.2 322C 321A Ciel and Robot
- CF 321A(Ciel and Robot-暴力枚举)
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- CF 322C. Ciel and Robot 枚举+模拟
- Codeforces Round #190 (Div. 2)-C. Ciel and Robot
- codeforces 322 A Ciel and Dancing
- codeforces 327 A Ciel and Dancing
- codeforces 322A. Ciel and Dancing
- Codeforces 321B Ciel and Duel KM
- Codeforces 321E Ciel and Gondolas(DP)
- Android 获取手机IMEI
- jstat命令
- JVM类加载机制
- 使用dom4j技术对xml文档进行增删改练习(一)
- Java动态代理实现原理浅析
- CodeForces 321A Ciel and Robot(模拟)
- Ubuntu Android 刷机
- ConcurrentModificationException
- Windows 10和Ubuntu 16.04双系统时间错误调整
- Linux-Ubuntu12.04_01 获取root权限
- flask + MongoDB 学习笔记: Exceptions, Errors and Solutions
- LeetCode:Contains Duplicate
- Java RuntimeException
- 表格的表示