Submission #703481

#TimeUsernameProblemLanguageResultExecution timeMemory
703481bebraNautilus (BOI19_nautilus)C++17
66 / 100
1066 ms210100 KiB
#include <bits/stdc++.h> using namespace std; #define dbg(x) cerr << #x << ": " << x << endl; const int MAX_N = 500 + 5; string a[MAX_N]; bool used[MAX_N][MAX_N][MAX_N]; bool res[MAX_N][MAX_N][MAX_N]; 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][pos]) { return res[i][j][pos]; } if (pos == (int)s.size()) { res[i][j][pos] = true; return true; } auto c = s[pos]; used[i][j][pos] = true; 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 { res[i][j][pos] |= can(i + 1, j, pos + 1, n, m, s); res[i][j][pos] |= can(i, j + 1, pos + 1, n, m, s); res[i][j][pos] |= can(i - 1, j, pos + 1, n, m, s); res[i][j][pos] |= can(i, j - 1, pos + 1, n, m, s); } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...