結果

問題 No.2848 Birthday Hit and Blow
ユーザー pockyny
提出日時 2025-08-18 03:34:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 6,531 bytes
コンパイル時間 1,094 ms
コンパイル使用メモリ 91,824 KB
実行使用メモリ 25,960 KB
平均クエリ数 703.50
最終ジャッジ日時 2025-08-18 03:34:48
合計ジャッジ時間 2,176 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:81:38: warning: ‘d’ may be used uninitialized [-Wmaybe-uninitialized]
   81 |                 if(st[i]=='x') st[i] = c;
main.cpp:63:18: note: ‘d’ was declared here
   63 |             char d;
      |                  ^

ソースコード

diff #

#include <iostream>
#include <string>
#include <cassert>
#include <vector>
#include <algorithm>

using namespace std;
bool isLocal = false;
string ans_str;
int c = 0;
pair<int,int> calc(string s){
    c++;
    assert(ans_str.size()==4);
    int i,j,h = 0,b = 0;
    for(i=0;i<s.size();i++){
        if(s[i]==ans_str[i]){
            h++;
        }else{
            for(j=0;j<4;j++){
                if(s[i]==ans_str[j]) b++;
            }
        }
    }
    return {h,b};
}
pair<int,int> query(string s){
    if(isLocal) return calc(s);
    cout << "? " << s << endl;
    int h,b; cin >> h >> b;
    return {h,b};
}

void ans(string s){
    if(isLocal){
        if(s!=ans_str){
            cout << "WA " << s << endl; 
        }
        assert(s==ans_str);
        return;
    }
    cout << "! " << s << endl;
    int x; cin >> x;   
}

void solve(){
    c = 0;
    int i,j;
    pair<int,int> p = query("0123");
    if(p.first + p.second==4){
        vector<string> v = {"0213","0312","0321","1023","1203"};
        string st = "1230";
        if(p.first==4) st = "0123";
        for(auto s:v){
            auto [h,_] = query(s);
            if(h==4) st = s;
        }
        ans(st);
    }else if(p.first + p.second==3){
        if(p.first==3){
            // 0x23 or 012x
            int h_mn = 4,c_mn = 4,c_mx = 0;
            char c = '9';
            char d;
            for(i=4;i<9;i++){
                string st = "0x23";
                string str = st;
                str[1] = (char)('0' + i);
                auto [h,b] = query(str);
                h_mn = min(h_mn,h);
                c_mn = min(c_mn,h + b);
                if(c_mx<h + b){
                    c_mx = h + b;
                    d = (char)('0' + i);
                }
            }
            string st = "";
            if(h_mn==3) st = "0x23";
            else st = "012x";
            if(c_mn<c_mx) c = d;
            for(i=0;i<4;i++){
                if(st[i]=='x') st[i] = c;
            }
            ans(st);
        }else if(p.first==2){
            char c = '9';
            vector<string> v = {"0x13","0x13","0x13","0x21","0x21"};
            vector<int> h,b;
            for(i=4;i<9;i++){
                char d = (char)('0' + i);
                string s = v[i - 4];
                for(j=0;j<4;j++){
                    if(s[j]=='x') s[j] = d;
                }
                auto [_h,_b] = query(s);
                h.push_back(_h); b.push_back(_b);
            }
            if(h[0] + b[0]>h[1] + b[1]) c = '4';
            if(h[1] + b[1]>h[0] + b[0]) c = '5';
            if(h[2] + b[2]>h[0] + b[0]) c = '6';
            if(h[3] + b[3]>h[4] + b[4]) c = '7';
            if(h[4] + b[4]>h[3] + b[3]) c = '8';
            string s = "032x";
            for(i=0;i<5;i++){
                int _h = h[i];
                if(c - '0' - 4==i) _h--;
                if(_h>=3) s = v[i];
            }
            for(i=0;i<4;i++){
                if(s[i]=='x') s[i] = c;
            }
            ans(s);
        }else if(p.first==1){
            char c = '9';
            vector<string> v = {"0x12","102x","021x","0x31","031x"};
            vector<int> h,b;
            for(i=4;i<9;i++){
                char d = (char)('0' + i);
                string s = v[i - 4];
                for(j=0;j<4;j++){
                    if(s[j]=='x') s[j] = d;
                }
                auto [_h,_b] = query(s);
                h.push_back(_h); b.push_back(_b);
            }
            if(h[0] + b[0]>h[1] + b[1]) c = '4';
            if(h[1] + b[1]>h[0] + b[0]) c = '5';
            if(h[2] + b[2]>h[0] + b[0]) c = '6';
            if(h[3] + b[3]>h[4] + b[4]) c = '7';
            if(h[4] + b[4]>h[3] + b[3]) c = '8';
            string s;
            for(i=0;i<5;i++){
                int _h = h[i];
                if(c - '0' - 4==i) _h--;
                if(_h>=3) s = v[i];
            }
            for(i=0;i<4;i++){
                if(s[i]=='x') s[i] = c;
            }
            ans(s);
        }else{
            string s = "1209";
            for(i=4;i<9;i++){
                string st = "120x";
                st[3] = (char)('0' + i);
                auto [h,_] = query(st);
                if(h==4) s = st;
            }
            ans(s);
        }
    }else if(p.first + p.second==2){
        auto [h1,b1] = query("0435");
        auto [h2,b2] = query("0637");
        int c1 = h1 + b1 - 1,c2 = h2 + b2 - 1;
        int c3 = 2 - c1 - c2;
        string X = "",Y = "";
        if(c1==2) X = "46", Y = "57";
        if(c2==2) X = "46", Y = "57";
        if(c3==2) X = "48", Y = "59";
        if(c1==1 && c2==1) X = "45", Y = "67";
        if(c1==1 && c3==1) X = "45", Y = "89";
        if(c2==1 && c3==1) X = "67", Y = "89";
        string ss = "0x3y";
        ss[1] = X[0]; ss[3] = Y[0];
        auto [h4,b4] = query(ss);
        ss[1] = X[0]; ss[3] = Y[1];
        auto [h5,b5] = query(ss);
        int c4 = h4 + b4 - 1,c5 = h5 + b5 - 1;
        string st = "0x1y";
        if(c4==2){
            if(h4==3) st[1] = X[0], st[3] = Y[0];
            else st[1] = Y[0], st[3] = X[0];
        }else if(c5==2){
            if(h5==3) st[1] = X[0], st[3] = Y[1];
            else st[1] = Y[1], st[3] = X[0];
        }else{
            if(c4<c5){
                if(h5>=2) st[1] = X[1], st[3] = Y[1];
                else st[1] = Y[1], st[3] = X[1];
            }else{
                if(h4>=2) st[1] = X[1], st[3] = Y[0];
                else st[1] = Y[0], st[3] = X[1];
            }
        }
        auto [h6,b6] = query(st);
        if(h6!=4) st[2] = '2';
        ans(st);
    }else{
        assert(false);
    }
    assert(c<=6);
}
int main(){
    int i,t; cin >> t;
    while(t){
        t--;
        solve();
    }
    // vector<int> month = {1,2,3,4,5,6,7,8,9,10,11,12};
    // vector<int> days = {31,28,31,30,31,30,31,31,30,31,30,31};
    // for(i=0;i<month.size();i++){
    //     for(int j=1;j<=days[i];j++){
    //         int num = 100*month[i] + j;
    //         string s;
    //         for(int k=0;k<4;k++){
    //             s.push_back((char)('0' + (num)%10));
    //             num /= 10;
    //         }
    //         reverse(s.begin(),s.end());
    //         bool f = false;
    //         for(int l=0;l<s.size();l++){
    //             for(int r=l + 1;r<s.size();r++){
    //                 if(s[l]==s[r]) f = true;
    //             }
    //         }
    //         if(f) continue;
    //         ans_str = s;
    //         cout << "check " << s << endl;
    //         solve();
    //     }
    // }
}
0