CSP-S 2023 T1 题解

发布时间 2023-11-09 09:43:42作者: beautiful_chicken233

CSP-S 2023 T1 题解

很简单,我们只需要暴力枚举五位密码,每次判断拨一个齿轮和两个齿轮能达到的状态数,如果等于 \(n\),答案 \(+1\)。时间复杂度 \(O(10^5 \times 5n)\)

code

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

using ll = long long;

const int kMaxN = 110, kInf = (((1 << 30) - 1) << 1) + 1, kMod = 1e9 + 7;

int n, a[9][6], b[6], ans = 0, sum = 0, cnt = 0, c[4], idx = 0;

int main() {
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++ i) {
    for (int j = 1; j <= 5; ++ j) {
      cin >> a[i][j];
    } 
  } 
  // 枚举五位密码
  for (int i = 0; i <= 9; ++ i) { 
    for (int j = 0; j <= 9; ++ j) {
      for (int k = 0; k <= 9; ++ k) {
        for (int l = 0; l <= 9; ++ l) {
          for (int x = 0; x <= 9; ++ x) {
            b[1] = i, b[2] = j, b[3] = k, b[4] = l, b[5] = x;
            sum = 0;
            // 拨一个齿轮能达到的状态
            for (int I = 1; I <= n; ++ I) {
              cnt = 0;
              for (int J = 1; J <= 5; ++ J) {
                (b[J] == a[I][J]) && (++ cnt);
              }
              (cnt == 4) && (++ sum);
            }
            // 拨两个齿轮能达到的状态
            for (int I = 1; I <= n; ++ I) {
              cnt = 0, idx = 0;
              for (int J = 1; J <= 5; ++ J) {
                if (b[J] == a[I][J]) { // 如果相等
                  ++ cnt; // 相同个数 +1
                } else { // 否则记录
                  c[(idx == 3? idx : ++ idx)] = J;
                }
              }
              if (cnt == 3) { // 如果有 2 个不同
                if (idx == 2 && abs(c[1] - c[2]) == 1) { // 如果相邻
                  if ((a[I][c[1]] < b[c[1]]? 10 - (b[c[1]] - a[I][c[1]]) : a[I][c[1]] - b[c[1]]) == (a[I][c[2]] < b[c[2]]? 10 - (b[c[2]] - a[I][c[2]]) : a[I][c[2]] - b[c[2]]) && (a[I][c[1]] > b[c[1]]? 10 - (a[I][c[1]] - b[c[1]]) : b[c[1]] - a[I][c[1]]) == (a[I][c[2]] > b[c[2]]? 10 - (a[I][c[2]] - b[c[2]]) : b[c[2]] - a[I][c[2]])) { // 如果能朝同一方向波动且幅度相同
                    ++ sum;
                  }
                }
              }
            } 
            if (sum == n) { // 如果能达到的个数 = n
              ++ ans; // 答案 +1
            }
          }
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}