• CodeForces 321A Ciel and Robot(数学模拟)


    题目链接:http://codeforces.com/problemset/problem/321/A

    题意:在一个二维平面中,開始时在(0,0)点,目标点是(a。b),问能不能通过反复操作题目中的指令,从原点移动到目标点。

    分析:如果一次完毕全部的命令后。移动到了(xx,yy),而且从(Xi。Yi)反复操作k次指令到达目标点。则能够列出方程 Xi + k * xx = a && Yi + k * yy = b。然后解出k。推断k是否大于等于0就可以。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef __int64 LL;
    LL a, b, xx, yy;
    LL X[150], Y[150];
    int len;
    bool check(LL x, LL y) {
        LL tmp_x = a - x, tmp_y = b - y;
        if(xx == 0) {
            if(yy == 0) {
                if(tmp_x == 0 && tmp_y == 0) return true;
                else return false;
            }
            else {
                if(tmp_y % yy == 0) {
                    if(tmp_y / yy >= 0 && tmp_x == 0) return true;
                    else return false;
                }
                else return false;
            }
        }
        else {
            if(yy == 0) {
                if(tmp_x % xx == 0) {
                    if(tmp_x / xx >= 0 && tmp_y == 0) return true;
                    else return false;
                }
                else return false;
            }
            else {
                if(tmp_x % xx == 0 && tmp_y % yy == 0) {
                    if(tmp_x / xx >= 0 && tmp_y / yy >= 0 && tmp_x / xx == tmp_y / yy) return true;
                    else return false;
                }
                else return false;
            }
        }
    }
    
    int main() {
        char op[150];
        while(~scanf("%I64d%I64d", &a, &b)) {
            scanf("%s", op);
            if(a == 0 && b == 0) {
                printf("Yes
    ");
                continue;
            }
            len = strlen(op);
            LL x = 0, y = 0;
            int FLAG = 0;
            for(int i = 0; i < len; i++) {
                if(op[i] == 'U') y++;
                else if(op[i] == 'D') y--;
                else if(op[i] == 'L') x--;
                else if(op[i] == 'R') x++;
                X[i] = x, Y[i] = y;
            }
            xx = X[len-1], yy = Y[len-1];
            for(int i = 0; i < len; i++) {
                if(check(X[i], Y[i])) {
                    FLAG = 1;
                    printf("Yes
    ");
                    break;
                }
            }
            if(!FLAG)  printf("No
    ");
        }
        return 0;
    }


  • 相关阅读:
    【LeetCode】1404. 将二进制表示减到 1 的步骤数
    【剑指Offer】面试题12. 矩阵中的路径(DFS)
    【LeetCode】994. 腐烂的橘子(BFS)
    【LeetCode】365. 水壶问题(BFS/裴蜀定理)
    【LeetCode】169. 多数元素(摩尔投票法)
    ASP.NET页面间传值
    SQL——基础概念
    SQL——登陆触发器实现限制IP
    SQL Server之null
    SQL Server服务器连接配置
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6852160.html
Copyright © 2020-2023  润新知