제출 #1156984

#제출 시각아이디문제언어결과실행 시간메모리
1156984htphong0909Nautilus (BOI19_nautilus)C++20
66 / 100
1095 ms15068 KiB
#include<bits/stdc++.h>
using namespace std;

int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };

bitset<1250250001> vis;
int n, m, h;

inline int ch(const int& a, const  int& b, const  int& c) {
    return (a - 1) * m + b + c * n * m;
}

char arr[501][501];

inline bool in(const int& x, const int& y) {
    return x > 0 && x <= n && y > 0 && y <= m && arr[x][y] == '.';
}

pair<int, int> di[101];

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    queue<array<int, 3>> q;
    cin >> n >> m >> h;
    di['N' - 'A'] = make_pair(-1, 0);
    di['S' - 'A'] = make_pair(1, 0);
    di['W' - 'A'] = make_pair(0, -1);
    di['E' - 'A'] = make_pair(0, 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> arr[i][j];
            if (arr[i][j] == '.') {
                q.push({i, j, 0});
                vis[ch(i, j, 0)] = true;
            }
        }
    }
    string s;
    cin >> s;
    s = '.' + s;
    int ans = 0;
    while (!q.empty()) {
        auto [ux, uy, uk] = q.front();
        q.pop();
        if (uk == h) {
            ans++;
            continue;
        }
        if (const int vk = uk + 1; s[vk] == '?') {
            for (int i = 0; i < 4; i++) {
                int vx = ux + dx[i];
                int vy = uy + dy[i];
                if (!in(vx, vy) || vis[ch(vx, vy, vk)]) continue;
                q.push({vx, vy, vk});
                vis[ch(vx, vy, vk)] = true;
            }
        }
        else {
            int vx = ux + di[s[vk] - 'A'].first;
            int vy = uy + di[s[vk] - 'A'].second;
            if (!in(vx, vy) || vis[ch(vx, vy, vk)]) continue;
            q.push({vx, vy, vk});
            vis[ch(vx, vy, vk)] = true;
        }
    }

    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...