• Ciel and Robot


    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.

    细节蛮多的,做的我好忧伤。。。

     1 #include <iostream>
     2 #include <string>
     3 #include <map>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <cstring>
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     char s[101];
    12     int a, b, dx, dy, i;
    13     while(scanf("%d %d", &a, &b) != EOF)
    14     {
    15         scanf("%s", s);
    16         dx = dy = 0;
    17         for(i = 0; s[i] != ''; i++)
    18         {
    19             if(dx == a && dy == b) break;
    20             if(s[i] == 'U') dy++;
    21             else if(s[i] == 'D') dy--;
    22             else if(s[i] == 'L') dx--;
    23             else dx++;
    24         }
    25         if(s[i] == '')
    26         {
    27             int dx2 = abs(dx), dy2 = abs(dy);
    28             for(i = 0; s[i] != ''; i++)
    29             {
    30                 if(s[i] == 'U') b--;
    31                 else if(s[i] == 'D') b++;
    32                 else if(s[i] == 'L') a++;
    33                 else a--;
    34                 if(!a && !b) break;
    35                 int a2 = abs(a), b2 = abs(b);
    36                 if((long long)a * dy == (long long)b * dx && (long long)b * dy >= 0 && (long long)a * dx >= 0)
    37                 {
    38                     if(dy && dx)
    39                     {
    40                         if(a2 % dx2 == 0 && b2 % dy2 == 0)  break;
    41                     }
    42                     else if(!dx && dy)
    43                     {
    44                         if(!a && b2 % dy2 == 0)  break;
    45                     }
    46                     else if(dx && !dy)
    47                     {
    48                         if(!b && a2 % dx2 == 0) break;
    49                     }
    50                 }
    51             }
    52         }
    53         if(s[i] != '') puts("Yes");
    54         else puts("No");
    55     }
    56     return 0;
    57 }
    View Code

  • 相关阅读:
    CppUnit使用指南
    详细分析内存泄露检测
    设计模式之我的理解创建型模式:工厂方法
    MSXML2使用笔记
    【C++】函数指针
    设计模式之我的理解桥模式
    python 使用pyinstaller,pywin32打包.py成.exe应用程序
    python 获取当前时间的用法
    python 使用urllib.urlopen超时问题的解决方法
    python pip的安装流程,以及使用pip更新,卸载第三方模块
  • 原文地址:https://www.cnblogs.com/cszlg/p/3242265.html
Copyright © 2020-2023  润新知