제출 #703562

#제출 시각아이디문제언어결과실행 시간메모리
703562bebraNautilus (BOI19_nautilus)C++17
66 / 100
1092 ms125596 KiB
#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];

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

bool dp[MAX_N][MAX_N][MAX_N];


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());

    vector<vector<pair<int, int>>> dir(t);
    for (int i = 0; i < t; ++i) {
        auto c = s[i];
        if (c == 'N') {
            dir[i] = {delta[0]};
        } else if (c == 'S') {
            dir[i] = {delta[1]};
        } else if (c == 'W') {
            dir[i] = {delta[2]};
        } else if (c == 'E') {
            dir[i] = {delta[3]};
        } else {
            dir[i] = delta;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            dp[i][j][t] = (a[i][j] == '#' ? 0 : 1);
        }
    }
    // reverse the operations and check if there exists at least one starting position
    for (int k = t - 1; k >= 0; --k) {
        for (const auto& [di, dj] : dir[k])  {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    if (a[i][j] == '#') continue;
                    if (i + di < 0 || i + di >= n || j + dj < 0 || j + dj >= m) continue;
                    dp[i][j][k] |= dp[i + di][j + dj][k + 1];
                }
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            ans += dp[i][j][0];
        }
    }

    cout << ans << '\n';

    
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...