Submission #703498

# Submission time Handle Problem Language Result Execution time Memory
703498 2023-02-27T14:09:10 Z bebra Nautilus (BOI19_nautilus) C++17
66 / 100
1000 ms 251496 KB
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cerr << #x << ": " << x << endl;

const int MAX_N = 500 + 5;
const int MAX_M = 5000 + 5;
string a[MAX_N];

bitset<MAX_M> used[MAX_N][MAX_N];
bitset<MAX_M> res[MAX_N][MAX_N];


vector<pair<int, int>> delta = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};


mt19937 rng(234);


bool can(int i, int j, int pos, int n, int m, const string& s) {
    if (i < 0 || i >= n || j < 0 || j >= m || a[i][j] == '#') {
        return false;
    }
    if (used[i][j].test(pos)) {
        return res[i][j].test(pos);
    }

    if (pos == (int)s.size()) {
        res[i][j].set(pos);
        return true;
    }

    auto c = s[pos];
    used[i][j].set(pos);

    if (c == 'N') {
        res[i][j][pos] = can(i - 1, j, pos + 1, n, m, s);
    } else if (c == 'S') {
        res[i][j][pos] = can(i + 1, j, pos + 1, n, m, s);
    } else if (c == 'W') {
        res[i][j][pos] = can(i, j - 1, pos + 1, n, m, s);
    } else if (c == 'E') {
        res[i][j][pos] = can(i, j + 1, pos + 1, n, m, s);
    } else {
        auto d = delta;
        shuffle(d.begin(), d.end(), rng);
        for (const auto& [di, dj] : d) {
            res[i][j].set(pos, can(i + di, j + dj, pos + 1, n, m, s));
            if (res[i][j].test(pos)) return true;
        }
    }
    return res[i][j][pos];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, t;
    cin >> n >> m >> t;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    for (auto& c : s) {
        if (c == 'N') {
            c = 'S';
        } else if (c == 'S') {
            c = 'N';
        } else if (c == 'W') {
            c = 'E';
        } else if (c == 'E') {
            c = 'W';
        }
    }
    int ans = 0;
    // reverse the operations and check if there exists at least one starting position
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (a[i][j] == '#') continue;
            if (can(i, j, 0, n, m, s)) ++ans;
        }
    }
    cout << ans << '\n';

    
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 14 ms 13396 KB Output is correct
2 Correct 12 ms 13396 KB Output is correct
3 Correct 8 ms 13268 KB Output is correct
4 Correct 8 ms 12756 KB Output is correct
5 Correct 6 ms 10068 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 13396 KB Output is correct
2 Correct 12 ms 13396 KB Output is correct
3 Correct 8 ms 13268 KB Output is correct
4 Correct 8 ms 12756 KB Output is correct
5 Correct 6 ms 10068 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 22 ms 13420 KB Output is correct
8 Correct 16 ms 13268 KB Output is correct
9 Correct 9 ms 12500 KB Output is correct
10 Correct 6 ms 10068 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 20 ms 13488 KB Output is correct
13 Correct 20 ms 13368 KB Output is correct
14 Correct 17 ms 13332 KB Output is correct
15 Correct 8 ms 10144 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 22 ms 13476 KB Output is correct
18 Correct 26 ms 13348 KB Output is correct
19 Correct 28 ms 12472 KB Output is correct
20 Correct 19 ms 10088 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 13396 KB Output is correct
2 Correct 12 ms 13396 KB Output is correct
3 Correct 8 ms 13268 KB Output is correct
4 Correct 8 ms 12756 KB Output is correct
5 Correct 6 ms 10068 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 22 ms 13420 KB Output is correct
8 Correct 16 ms 13268 KB Output is correct
9 Correct 9 ms 12500 KB Output is correct
10 Correct 6 ms 10068 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 20 ms 13488 KB Output is correct
13 Correct 20 ms 13368 KB Output is correct
14 Correct 17 ms 13332 KB Output is correct
15 Correct 8 ms 10144 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 22 ms 13476 KB Output is correct
18 Correct 26 ms 13348 KB Output is correct
19 Correct 28 ms 12472 KB Output is correct
20 Correct 19 ms 10088 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Execution timed out 1074 ms 251496 KB Time limit exceeded
23 Halted 0 ms 0 KB -