[CF321A] Ciel and Robot

Description

你现在在一个迷宫的 \((0,0)\) 处,给定一个包含 \(\texttt{U,D,L,R}\) 的操作序列 \(s\),其中 \(\texttt{U}\) 表示向上走一格,\(\texttt{D}\) 表示向下走一格,\(\texttt{L}\) 表示向左走一格,\(\texttt{R}\) 表示向右走一格。你将会按照 \(s\) 从左往右的操作移动,并且重复若干次。问你是否能够到达 \((a,b)\) 处。

Solution

将一轮中能到达的所有位置即为 \(p_1,p_2,...,p_i\),目标位置记为 \(target\),每做完一轮的偏移量为 \(offset\)。若存在 \(i\),使得 \(offset | (target-p_i)\),则输出 Yes。

#include <bits/stdc++.h>
using namespace std;

#define int long long

struct vec2
{
    int x, y;

    vec2() : x(0), y(0)
    {
    }

    vec2(int x, int y) : x(x), y(y)
    {
    }

    bool div(const vec2 &rhs) const
    {
        if (x == 0 && rhs.x == 0 && y == 0 && rhs.y == 0)
            return true;
        if (x == 0 && rhs.x == 0 && y != 0 && rhs.y % y == 0 && rhs.y / y >= 0)
            return true;
        if (y == 0 && rhs.y == 0 && x != 0 && rhs.x % x == 0 && rhs.x / x >= 0)
            return true;
        if (x == 0 || y == 0)
            return false;
        if (rhs.x % x == 0 && rhs.y % y == 0)
        {
            int dx = rhs.x / x, dy = rhs.y / y;
            if (dx == dy && dx >= 0)
                return true;
        }
        return false;
    }

    vec2 operator+(const vec2 &rhs) const
    {
        return {x + rhs.x, y + rhs.y};
    }

    vec2 operator-(const vec2 &rhs) const
    {
        return {x - rhs.x, y - rhs.y};
    }
};

signed main()
{
    vec2 target;
    cin >> target.x >> target.y;

    string str;
    cin >> str;

    map<char, vec2> walk;
    walk['L'] = vec2(-1, 0);
    walk['R'] = vec2(+1, 0);
    walk['U'] = vec2(0, +1);
    walk['D'] = vec2(0, -1);

    vector<vec2> p;
    vec2 now;

    p.push_back(now);
    for (int i = 0; i < str.length(); i++)
    {
        now = now + walk[str[i]];
        p.push_back(now);
    }

    bool ans = 0;
    for (auto x : p)
    {
        auto y = target - x;
        if (now.div(y))
            ans = true;
    }

    if (ans)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-18
  • 2022-12-23
  • 2021-05-20
  • 2022-01-14
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
相关资源
相似解决方案