#include <bits/stdc++.h>
using namespace std;
static unsigned char cur[500][500];
static unsigned char nxt[500][500];
static unsigned char blocked[500][500];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int r, c, m;
cin >> r >> c >> m;
// read grid
for (int i = 0; i < r; i++) {
string s;
cin >> s;
for (int j = 0; j < c; j++) {
blocked[i][j] = (s[j] == '#');
cur[i][j] = !blocked[i][j]; // start anywhere not blocked
}
}
string way;
cin >> way;
for (int step = 0; step < m; step++) {
char w = way[step];
memset(nxt, 0, sizeof(nxt));
if (w == '?') {
// propagate all 4 directions
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
if (cur[i][j]) {
if (i) nxt[i-1][j] = 1;
if (i+1 < r) nxt[i+1][j] = 1;
if (j) nxt[i][j-1] = 1;
if (j+1 < c) nxt[i][j+1] = 1;
}
}
else if (w == 'N') {
for (int i = 0; i+1 < r; i++)
for (int j = 0; j < c; j++)
nxt[i][j] = cur[i+1][j];
}
else if (w == 'S') {
for (int i = 1; i < r; i++)
for (int j = 0; j < c; j++)
nxt[i][j] = cur[i-1][j];
}
else if (w == 'E') {
for (int i = 0; i < r; i++)
for (int j = 1; j < c; j++)
nxt[i][j] = cur[i][j-1];
}
else if (w == 'W') {
for (int i = 0; i < r; i++)
for (int j = 0; j+1 < c; j++)
nxt[i][j] = cur[i][j+1];
}
// apply obstacles + detect emptiness
bool any = false;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
nxt[i][j] &= !blocked[i][j];
any |= nxt[i][j];
}
}
if (!any) {
cout << 0;
return 0;
}
// swap buffers
memcpy(cur, nxt, sizeof(cur));
}
int ans = 0;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
ans += cur[i][j];
cout << ans;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |