Submission #996393

#TimeUsernameProblemLanguageResultExecution timeMemory
996393yanbSnake Escaping (JOI18_snake_escaping)C++14
0 / 100
2041 ms856 KiB
#include <bits/stdc++.h>
    
using namespace std;
    
#define int long long
#define pii pair<long long, long long>
#define t3i tuple<long long, long long, long long>

int build(vector<int> &tri, vector<int> &ans, string &tox, int l, int q) {
    int v = 0;
    for (int i = 0; i < l; i++) {
        v *= 3;
        v += tri[i];
    }

    bool ok = 0;
    for (int i = 0; i < l; i++) {
        if (tri[i] == 2) {
            int cans = 0;
            tri[i] = 0;
            cans += build(tri, ans, tox, l, q);
            tri[i] = 1;
            cans += build(tri, ans, tox, l, q);
            tri[i] = 2;
            ans[v] = cans;
            ok = 1;
        }
    }

    if (!ok) {
        int bin = 0;
        for (int i = 0; i < l; i++) {
            bin *= 2;
            bin += tri[i];
        }
        ans[v] = tox[bin] - '0';
    }

    return ans[v];
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int l, q;
    string tox;
    cin >> l >> q >> tox;
    
    vector<int> ans(pow(3, l) + 100);
    vector<int> tri(l, 2);
    build(tri, ans, tox, l, q);

    while (q--) {
        string s;
        cin >> s;
        int v = 0;
        for (int i = 0; i < l; i++) {
            v *= 3;
            v += (s[i] == '?' ? 2 : s[i] - '0');
        }
        cout << ans[v] << "\n";
    }
}   
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...