Codeforces: Round 190 div 2 C

Problem:
==============================================================================================================
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.

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).

==============================================================================================================

这题犯了几个错误:

1. set.insert操作是有<操作符的,所以set里如果是一个自己定义的新结构那么要对这个新结构做<符号重载。这点在做这题的过程中花了我很长时间去找原因,根本原因还是对stl的函数原理理解不够。

2. 一开始的思路错误,本来的想法是做一个for循环,i=0到i=max(des.x, des.y)+s.size()+1,循环里对每个走到的点问问是不是终点。显然这个思路对大数据不通过。

3. move.x和move.y == 0的情况没有考虑,这样总共分4种情况。一开始就是简单无脑地没有考虑 == 0的情况

4. 大数据的验证要注意数据溢出,一开始忘记加1ll。

5. 没考虑到move的方向与des的方向得契合,所以要有des.y*1ll*move.y >= 0这个条件

这些错误在debug的时候一次次地出现,搞得我也很抓狂。自己的思路还是很不够严谨的。

现在说下主要的思路:

收集每个s动作过后能踩到的点集,这里为set<node> scope,然后要算出这个s动作过后点最终在什么点,这里为move,那么每次的s动作过后点的方向就是move了,原点为0,0,那么经过n次地s动作后所能踩到的点集如果要在终点处,则须满足:

x(i)+n*move.x = des.x;

y(i)+n*move.y = des.y;

解这个方程则为(des.x-x(i))*move.y = (des.x-y(i))*move.x,注意(des.x-x(i)) % move.x == 0.

经验:数学是做算法题的好帮手,数学好可以做很多pre-job,可以给程序带来很多便捷。开始做优化的时候尽量多做些数学的工作。

最后贴代码段

 1 #include <iostream>

 2 #include <fstream>

 3 #include <string>

 4 #include <map>

 5 #include <vector>

 6 #include <set>

 7 #include <algorithm>

 8 #include <queue>

 9 #include <math.h>

10 

11 #define modulo 1000000007

12 

13 using namespace std;

14 

15 struct node {

16     int x;

17     int y;

18     node(int a, int b) : x(a), y(b) { }

19     node() : x(0), y(0) { }

20 

21 };

22 

23 bool operator<(const node &a, const node &b) {

24     return a.x == b.x? a.y < b.y : a.x < b.x;

25 }

26 

27 int main() {

28     map<char, int> dir;

29     dir['U'] = 0;

30     dir['D'] = 1;

31     dir['L'] = 2;

32     dir['R'] = 3;

33     int dirdir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

34     node des;

35     cin >> des.x >> des.y;

36     string s;

37     cin >> s;

38     set<node> scope;

39     scope.insert(node(0, 0));

40     node move(0, 0);

41     for (int i = 0; i < s.size(); i++) {

42         move.x += dirdir[dir[s[i]]][0];

43         move.y += dirdir[dir[s[i]]][1];

44         scope.insert(move);

45     }

46     node source(0, 0);

47     bool flag = false;

48     for (set<node>::iterator it = scope.begin(); it != scope.end(); it++) {

49         if (move.x == 0 && move.y == 0) flag |= ((*it).x == des.x && (*it).y == des.y);

50         else if (move.x == 0 && move.y != 0) flag |= ((*it).x == des.x && (des.y-(*it).y)%move.y == 0 && des.y*1ll*move.y >= 0);

51         else if (move.x != 0 && move.y == 0) flag |= ((*it).y == des.y && (des.x-(*it).x)%move.x == 0 && des.x*1ll*move.x >= 0);

52         else if (((des.x-(*it).x)*1ll*move.y == (des.y-(*it).y)*1ll*move.x) && (des.x-(*it).x)%move.x == 0 && (des.y-(*it).y)%move.y == 0 && (des.x-(*it).x)*1ll*move.x >= 0) flag = true;

53         if (flag) break;

54     }

55     if (flag) cout << "Yes" << endl;

56     else cout << "No" << endl;

57 

58     return 0;

59 }

 

你可能感兴趣的:(codeforces)