QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#549209#2345. Karel the RobotHKOI0AC ✓6891ms96044kbC++204.4kb2024-09-06 12:26:312024-09-06 12:26:31

Judging History

This is the latest submission verdict.

  • [2024-09-06 12:26:31]
  • Judged
  • Verdict: AC
  • Time: 6891ms
  • Memory: 96044kb
  • [2024-09-06 12:26:31]
  • Submitted

answer

#include <bits/stdc++.h>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (auto x : v) os << x << ' ';
    os << '\n';
    return os;
}

const vector<char> dirc{'n', 'w', 's', 'e'};
const vector<pii> dird{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

struct state {
    int x, y;
    char dir;
};

int to_state_num(state st) { return (st.x - 1) * 160 + (st.y - 1) * 4 + find(all(dirc), st.dir) - dirc.begin(); }

int dir_to_int(char dir) {
    assert(dir != '\0');
    return find(all(dirc), dir) - dirc.begin();
}

void solve() {
    int n, m, d, e;
    cin >> n >> m >> d >> e;
    vector<string> g(n + 2);
    for (int i = 1; i <= n; i++) {
        cin >> g[i];
        g[i] = '#' + g[i] + '#';
    }
    g[0] = g[n + 1] = string(m + 2, '#');
    vector strs(200, string());
    for (int i = 1; i <= d; i++) {
        string s;
        cin >> s;
        char c = s[0];
        strs[c] = s.substr(2);
    }

    map<pair<string, int>, state> val;
    set<pair<string, int>> vis;

    auto eval = [&](auto&& eval, string s, state st) -> state {
        int num = to_state_num(st);
        if (val.find({s, num}) != val.end()) return val[{s, num}];
        if (vis.find({s, num}) != vis.end()) {
            return val[{s, num}] = {-1, -1, '\0'};
        }
        if (s.empty()) return st;
        vis.emplace(s, num);

        auto get_pair = [&](int l) {
            int cnt = 0;
            for (int j = l; j < sz(s); j++) {
                if (s[j] == '(') cnt++;
                else if (s[j] == ')') cnt--;
                if (cnt == 0) return j;
            }
            assert(false);
        };

        for (int i = 0; i < sz(s); i++) {
            if (s[i] == 'm') {
                auto [dx, dy] = dird[dir_to_int(st.dir)];
                if (g[st.x + dx][st.y + dy] == '.') {
                    st.x += dx;
                    st.y += dy;
                }
            }
            else if (s[i] == 'l') {
                st.dir = dirc[(dir_to_int(st.dir) + 1) % 4];
            }
            else if (s[i] == 'i' || s[i] == 'u') {
                char c = s[i + 1];

                auto check = [&] {
                    if (c == 'b') {
                        auto [dx, dy] = dird[dir_to_int(st.dir)];
                        if (g[st.x + dx][st.y + dy] == '#') return true;
                        return false;
                    }
                    else {
                        return st.dir == c;
                    }
                };

                if (s[i] == 'i') {
                    int l1 = i + 2;
                    int r1 = get_pair(l1);
                    int l2 = r1 + 1;
                    int r2 = get_pair(l2);
                    string prog1 = s.substr(l1 + 1, r1 - l1 - 1);
                    string prog2 = s.substr(l2 + 1, r2 - l2 - 1);
                    if (check()) st = eval(eval, prog1, st);
                    else st = eval(eval, prog2, st);
                    i = r2;
                }
                else {
                    int l = i + 2;
                    int r = get_pair(l);
                    string prog = s.substr(l + 1, r - l - 1);
                    if (!check()) {
                        st = eval(eval, prog, st);
                        if (st.x == -1 || st.y == -1 || st.dir == '\0') return val[{s, num}] = {-1, -1, '\0'};
                        st = eval(eval, s.substr(i, r - i + 1), st);
                    }
                    i = r;
                }
            }
            else {
                st = eval(eval, strs[s[i]], st);
            }
            if (st.x == -1 || st.y == -1 || st.dir == '\0') return val[{s, num}] = {-1, -1, '\0'};
        }

        return val[{s, num}] = st;
    };

    for (int i = 1; i <= e; i++) {
        int x, y;
        char dir;
        cin >> x >> y >> dir;
        string s;
        cin >> s;
        auto st = eval(eval, s, {x, y, dir});
        if (st.x == -1) cout << "inf\n";
        else cout << st.x << ' ' << st.y << ' ' << st.dir << '\n';
    }
}

signed main() {
#ifndef LOCAL
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    cout << fixed << setprecision(10);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3632kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 3504kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 3848kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 3884kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 3580kb

Test #6:

score: 0
Accepted
time: 3ms
memory: 3928kb

Test #7:

score: 0
Accepted
time: 278ms
memory: 9184kb

Test #8:

score: 0
Accepted
time: 31ms
memory: 13312kb

Test #9:

score: 0
Accepted
time: 6891ms
memory: 67776kb

Test #10:

score: 0
Accepted
time: 0ms
memory: 3588kb

Test #11:

score: 0
Accepted
time: 0ms
memory: 3808kb

Test #12:

score: 0
Accepted
time: 0ms
memory: 3812kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 3664kb

Test #14:

score: 0
Accepted
time: 1ms
memory: 3652kb

Test #15:

score: 0
Accepted
time: 7ms
memory: 4856kb

Test #16:

score: 0
Accepted
time: 10ms
memory: 6612kb

Test #17:

score: 0
Accepted
time: 6521ms
memory: 67768kb

Test #18:

score: 0
Accepted
time: 6636ms
memory: 66956kb

Test #19:

score: 0
Accepted
time: 76ms
memory: 14008kb

Test #20:

score: 0
Accepted
time: 1134ms
memory: 96044kb