2 条题解

  • 0
    @ 2023-8-8 15:52:45

    暴搜就是了,时间复杂度 Θ(2n)\Theta(2^n)

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int b[9],w[9];
    bool vis[9];//0黑1白
    void dfs(int step,int bl,int wh){
        if (!step){
            bool f=1;
            for (int i=1;i<=n;i++){
                if (vis[i]){
                    f=0;
                }
                if (vis[i]&&(w[i]!=wh-1||b[i]!=bl)||!vis[i]&&w[i]==wh&&b[i]==bl-1){//白的说谎/黑的没说谎
                    return;
                }
            }
            if (f){//全黑
                cout<<0;
                exit(0);
            }
            for (int i=1;i<=n;i++){
                if (vis[i]){
                    cout<<i;
                }
            }
            exit(0);
        }
        vis[step]=0;
        dfs(step-1,bl+1,wh);
        vis[step]=1;
        dfs(step-1,bl,wh+1);
    }
    int main(){
        cin>>n;
        for (int i=1;i<=n;i++){
            cin>>w[i]>>b[i];
        }
        dfs(n,0,0);//倒序枚举,维护字典序
        cout<<"NoSolution.";
        return 0;
    }
    
    • 0
      @ 2021-9-27 10:32:29

      敲完一次过。这不科学。

      #include <bits/stdc++.h>
      using namespace std;
      int main() {
          int n;
          cin >> n;
          int w[n], b[n], res = 1e9;
          for (int i = 0; i < n; i++) cin >> w[i] >> b[i];
          for (int i = 0; i < (1 << n); i++) {
              int index = 0, t = i, color[n];
              for (int j = 0; j < n; j++) {
                  color[j] = (t & 1);
                  t        = t >> 1;
              }
              int total_white = accumulate(color, color + n, 0);
              bool fit        = true;
              for (int j = 0; j < n; j++) {
                  if (color[j]) {
                      if (total_white - 1 != w[j] or n - total_white != b[j])
                          fit = false;
                  } else {
                      if (total_white == w[j] and n - total_white - 1 == b[j])
                          fit = false;
                  }
              }
              if (fit) {
                  int result = 0;
                  for (int j = 0; j < n; j++) {
                      if (color[j]) {
                          result *= 10;
                          result += j + 1;
                      }
                  }
                  res = min(res, result);
              }
          }
          if (res == 1e9)
              cout << "NoSolution";
          else
              cout << res;
      }
      
      
      • 1

      信息

      ID
      861
      时间
      1000ms
      内存
      256MiB
      难度
      1
      标签
      递交数
      25
      已通过
      24
      上传者