3 条题解

  • 0
    @ 2026-10-13 19:30:31

    满分解法(100 分)

    直接枚举起点并逐条执行全部指令需要 O(n2)O(n^2) 时间。不同起点执行的是相同环形序列的不同切分,因此关键是让一整段指令的效果能够快速合并。

    用位移和旋转表示一段指令

    规定朝向编号 0,1,2,30,1,2,3 依次表示北、东、南、西。记

    T(S)=(xS,yS,dS),T(S)=(x_S,y_S,d_S),

    表示机器人从原点出发、初始面向北方,执行指令段 SS 后产生位移 (xS,yS)(x_S,y_S),并顺时针旋转 dSd_S 个 90∘90^\circ。只需保留 dS mod 4d_S\bmod 4。

    记 rot⁡d(x,y)\operatorname{rot}_d(x,y) 为把向量 (x,y)(x,y) 顺时针旋转 dd 个 90∘90^\circ。若机器人执行 SS 前已经转过 dd,那么 SS 的位移也会整体旋转为 rot⁡d(xS,yS)\operatorname{rot}_d(x_S,y_S);位移与起点坐标无关,所以这三个量足以描述该段的全部后续影响。

    单条指令的效果分别为

    $$T(\texttt{F})=(0,1,0),\qquad T(\texttt{L})=(0,0,3),\qquad T(\texttt{R})=(0,0,1).$$

    合并相邻两段

    设 T(A)=(xA,yA,dA)T(A)=(x_A,y_A,d_A),T(B)=(xB,yB,dB)T(B)=(x_B,y_B,d_B)。先执行 AA 后,执行 BB 时的初始朝向已经顺时针转过 dAd_A,所以 BB 的实际位移要随之旋转。于是

    $$T(AB)=\left((x_A,y_A)+\operatorname{rot}_{d_A}(x_B,y_B),\ (d_A+d_B)\bmod 4\right).$$

    这个式子完全复现了依次执行两段指令的过程,因此反复合并的结果就是整段指令的真实效果;执行顺序也由此得到保留。

    代码中的 combine(a,b) 正是计算先执行 a、再执行 b 的效果。向量顺时针转一次时,(x,y)(x,y) 变成 (y,−x)(y,-x),最多重复三次即可。

    前后缀回答全部起点

    令 prei=T(s1s2⋯si)pre_i=T(s_1s_2\cdots s_i),并令 pre0pre_0 为空串效果 (0,0,0)(0,0,0)。从左到右用合并公式求出全部前缀。

    同理,令 sufi=T(sisi+1⋯sn)suf_i=T(s_is_{i+1}\cdots s_n),并令 sufn+1=(0,0,0)suf_{n+1}=(0,0,0)。从右到左计算时,应把单条 sis_i 放在已有后缀 si+1⋯sns_{i+1}\cdots s_n 之前,不能交换合并顺序。

    若从第 ii 条指令开始,实际执行串为

    sisi+1⋯sns1s2⋯si−1.s_is_{i+1}\cdots s_ns_1s_2\cdots s_{i-1}.

    它恰好是后缀 si⋯sns_i\cdots s_n 后接前缀 s1⋯si−1s_1\cdots s_{i-1}。所以合并 sufisuf_i 与 prei−1pre_{i-1},所得三元组的前两个分量就是这个起点的最终坐标。

    枚举 nn 个起点并收集坐标。排序后删去相邻的重复坐标,剩余数量即为不同最终位置数。所有循环移位包含完全相同的转向指令,最终朝向虽然一致,但题目只统计坐标,不能把朝向加入去重键。

    正确性说明

    单条指令的三元组与题意一致。合并公式在执行后一段前,准确地按前一段造成的朝向旋转其位移,再相加位移和净旋转,因此合并后的三元组与依次执行两段完全等价。

    前缀和后缀均由这个等价合并递推得到,所以各自表示对应连续指令段的真实效果。对每个 ii,suf[i] 后接 pre[i] 恰好包含从 sis_i 开始的全部 nn 条指令且顺序正确,因而计算出的坐标就是该起点的终点。最终对这 nn 个真实终点去重,得到且只得到题目要求的不同位置数。

    复杂度

    预处理和枚举起点共进行 O(n)O(n) 次常数时间合并;对 nn 个坐标排序去重需要 O(nlog⁡n)O(n\log n) 时间。因此总时间复杂度为 O(nlog⁡n)O(n\log n),空间复杂度为 O(n)O(n)。

    AC 代码(C++20)

    #include <bits/stdc++.h>
    using namespace std;
    using i64 = long long;
    
    struct Transform {
      int x, y, d;
    };
    
    pair<int, int> rotate(int x, int y, int d) {
      while (d--) tie(x, y) = pair(y, -x);
      return {x, y};
    }
    
    Transform combine(Transform a, Transform b) {
      auto [x, y] = rotate(b.x, b.y, a.d);
      return {a.x + x, a.y + y, (a.d + b.d) % 4};
    }
    
    Transform instruction(char c) {
      if (c == 'F') return {0, 1, 0};
      if (c == 'L') return {0, 0, 3};
      return {0, 0, 1};
    }
    
    void solve() {
      int n;
      string s;
      cin >> n >> s;
      vector<Transform> pre(n + 1), suf(n + 1);
      for (int i = 0; i < n; ++i) pre[i + 1] = combine(pre[i], instruction(s[i]));
      for (int i = n - 1; i >= 0; --i) suf[i] = combine(instruction(s[i]), suf[i + 1]);
    
      vector<pair<int, int>> positions;
      positions.reserve(n);
      for (int i = 0; i < n; ++i) {
        Transform cur = combine(suf[i], pre[i]);
        positions.push_back({cur.x, cur.y});
      }
      sort(positions.begin(), positions.end());
      positions.erase(unique(positions.begin(), positions.end()), positions.end());
      cout << positions.size() << '\n';
    }
    
    int main() {
      cin.tie(0)->sync_with_stdio(0);
      solve();
    }
    
    • 0
      @ 2026-10-13 19:30:31

      利用零总转向化简(通过子任务 13–14,共 10 分)

      设从原起点执行完整指令串后,位移为向量 vv。本档保证最终朝向与初始朝向相同,即整串的净旋转为 00。

      考虑把指令串在第 ii 条指令前切成前缀 AA 和后缀 BB。设执行 AA 后的位移为 pp,净旋转为 dd;设从面向北方开始执行 BB 的位移为 qq。原顺序先执行 AA 再执行 BB,所以

      p+rot⁡d(q)=v.p+\operatorname{rot}_d(q)=v.

      整串净旋转为 00,因此 BB 的净旋转为 −d-d。若改为从第 ii 条指令开始,就会先执行 BB 再执行 AA,终点位移为

      $$q+\operatorname{rot}_{-d}(p) =\operatorname{rot}_{-d}(v-p)+\operatorname{rot}_{-d}(p) =\operatorname{rot}_{-d}(v).$$

      可见终点只由切分处前缀的朝向 dd 决定,与前缀位置 pp 无关。先模拟完整串求出 vv,再扫描一次字符串,维护每个起点前的前缀净旋转,把 vv 反向旋转相应次数并放入集合即可。旋转只有四种,集合至多包含四个坐标。

      时间复杂度为 O(n)O(n),额外空间复杂度为 O(1)O(1)。

      参考代码(C++20)

      #include <bits/stdc++.h>
      using namespace std;
      using i64 = long long;
      
      pair<int, int> rotate(int x, int y, int d) {
        while (d--) tie(x, y) = pair(y, -x);
        return {x, y};
      }
      
      void solve() {
        int n;
        string s;
        cin >> n >> s;
        int x = 0, y = 0, d = 0;
        for (char c : s) {
          if (c == 'F') {
            auto [dx, dy] = rotate(0, 1, d);
            x += dx;
            y += dy;
          } else if (c == 'L') {
            d = (d + 3) % 4;
          } else {
            d = (d + 1) % 4;
          }
        }
      
        set<pair<int, int>> positions;
        d = 0;
        for (char c : s) {
          positions.insert(rotate(x, y, (4 - d) % 4));
          if (c == 'L') d = (d + 3) % 4;
          if (c == 'R') d = (d + 1) % 4;
        }
        cout << positions.size() << '\n';
      }
      
      int main() {
        cin.tie(0)->sync_with_stdio(0);
        solve();
      }
      
      • 0
        @ 2026-10-13 19:30:30

        枚举起点逐步模拟(通过子任务 1–12,共 60 分)

        在 n≤2000n\le 2000 时,可以直接按照题意枚举起点。

        固定起点 startstart 后,把位置初始化为 (0,0)(0,0),朝向初始化为北。依次执行 nn 步,第 stepstep 步读取的指令下标是 (start+step) mod n(start+step)\bmod n:遇到 F 就按当前朝向更新坐标,遇到 L 或 R 就把朝向编号对 44 减一或加一。

        这段模拟逐条执行了从 startstart 开始的环形指令序列,因此得到的坐标正是该起点的终点。把每个终点放入集合,最终集合大小就是不同位置数。

        共有 nn 个起点,每个起点模拟 nn 条指令,时间复杂度为 O(n2log⁡n)O(n^2\log n)(集合插入至多进行 nn 次),空间复杂度为 O(n)O(n)。在 n≤2000n\le 2000 的测试点内可以通过。

        参考代码(C++20)

        #include <bits/stdc++.h>
        using namespace std;
        using i64 = long long;
        
        void solve() {
          int n;
          string s;
          cin >> n >> s;
          set<pair<int, int>> positions;
          const int dx[4] = {0, 1, 0, -1};
          const int dy[4] = {1, 0, -1, 0};
          for (int start = 0; start < n; ++start) {
            int x = 0, y = 0, d = 0;
            for (int step = 0; step < n; ++step) {
              char c = s[(start + step) % n];
              if (c == 'F') {
                x += dx[d];
                y += dy[d];
              } else if (c == 'L') {
                d = (d + 3) % 4;
              } else {
                d = (d + 1) % 4;
              }
            }
            positions.insert({x, y});
          }
          cout << positions.size() << '\n';
        }
        
        int main() {
          cin.tie(0)->sync_with_stdio(0);
          solve();
        }
        
        • 1

        信息

        ID
        2315
        时间
        1000ms
        内存
        1024MiB
        难度
        9
        标签
        (无)
        递交数
        63
        已通过
        17
        上传者