#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
#define st first
#define nd second
#define file "test"
#define rep(i, begin, end) for (ll i = (begin); i <= (end); i ++)
#define rrep(i, begin, end) for (ll i = (begin); i >= (end); i --)
using namespace std;
const long long INF = 1e18;
const long long N = 500 + 5;
char a[N][N];
string s;
int R, C, M;
struct node {
int x, y, i;
node(int _x, int _y, int _i) {
x = _x; y = _y; i = _i;
}
bool legal() {
return x >= 1 && x <= R && y >= 1 && y <= C && i <= M && a[x][y] == '.';
}
};
namespace sub12 {
int dr[] = {-1, 1, 0, 0}, dc[] = {0, 0, -1, 1};
int dir[300];
bool dp[N][N][N];
void init() {
dir['N'] = 0;
dir['S'] = 1;
dir['E'] = 3;
dir['W'] = 2;
dir['?'] = -1;
}
node nxt(node x, char ch) {
int d = dir[ch];
x.x += dr[d];
x.y += dc[d];
x.i ++;
return x;
}
void solve() {
init();
queue <node> q;
string direction = "WENS";
rep(i, 1, R) rep(j, 1, C) if (a[i][j] == '.')
q.push(node(i, j, 0)), dp[i][j][0] = 1;
while (q.size()) {
node u = q.front(); q.pop();
if (u.i == M) continue;
char ch = s[u.i];
for (char x: direction) if (ch == '?' || x == ch) {
node v = nxt(u, x);
if (v.legal()) {
if (!dp[v.x][v.y][v.i]) {
dp[v.x][v.y][v.i] = true;
q.push(v);
}
}
}
}
int ans = 0;
rep(i, 1, R) rep(j, 1, C) if (dp[i][j][M]) ans ++;
cout << ans << '\n';
}
};
namespace sub3 { // using bitset to speed up dp
const long long Max = 5e3 + 5;
bitset <N> sea[N], dp[N][Max];
void init() {
rep(i, 1, R) rep(j, 1, C) if (a[i][j] == '.')
sea[i].set(j);
// init each row of the sea
}
string direction = "WESN";
void solve() {
// E, W move in the same row -> (>> 1) or (<< 1)
init();
rep(i, 1, R)
dp[i][0] = sea[i];
rep(t, 0, M - 1) {
for (char ch: direction) if (ch == s[t] || s[t] == '?') {
rep(i, 1, R) {
if (ch == 'W')
dp[i][t + 1] |= (dp[i][t] >> 1) & sea[i];
if (ch == 'E')
dp[i][t + 1] |= (dp[i][t] << 1) & sea[i];
if (ch == 'S')
dp[i + 1][t + 1] |= dp[i][t] & sea[i + 1];
if (ch == 'N')
dp[i - 1][t + 1] |= dp[i][t] & sea[i - 1];
}
}
}
ll ans = 0;
rep(i, 1, R) ans += dp[i][M].count();
cout << ans << '\n';
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
// #endif
cin >> R >> C >> M;
rep(i, 1, R) rep(j, 1, C)
cin >> a[i][j];
cin >> s;
// if (M <= 500)
// sub12 :: solve();
// else
sub3 :: solve();
return 0;
}
Compilation message
nautilus.cpp: In function 'node sub12::nxt(node, char)':
nautilus.cpp:45:21: warning: array subscript has type 'char' [-Wchar-subscripts]
45 | int d = dir[ch];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1500 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1500 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1496 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1496 KB |
Output is correct |
14 |
Correct |
2 ms |
1496 KB |
Output is correct |
15 |
Correct |
2 ms |
1496 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
1492 KB |
Output is correct |
18 |
Correct |
2 ms |
1492 KB |
Output is correct |
19 |
Correct |
2 ms |
1492 KB |
Output is correct |
20 |
Correct |
2 ms |
1492 KB |
Output is correct |
21 |
Correct |
2 ms |
1496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1500 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1496 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1496 KB |
Output is correct |
14 |
Correct |
2 ms |
1496 KB |
Output is correct |
15 |
Correct |
2 ms |
1496 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
1492 KB |
Output is correct |
18 |
Correct |
2 ms |
1492 KB |
Output is correct |
19 |
Correct |
2 ms |
1492 KB |
Output is correct |
20 |
Correct |
2 ms |
1492 KB |
Output is correct |
21 |
Correct |
2 ms |
1496 KB |
Output is correct |
22 |
Correct |
132 ms |
158188 KB |
Output is correct |
23 |
Correct |
132 ms |
158132 KB |
Output is correct |
24 |
Correct |
140 ms |
158156 KB |
Output is correct |
25 |
Correct |
170 ms |
158168 KB |
Output is correct |
26 |
Correct |
130 ms |
158116 KB |
Output is correct |
27 |
Correct |
169 ms |
158092 KB |
Output is correct |
28 |
Correct |
173 ms |
158148 KB |
Output is correct |
29 |
Correct |
181 ms |
158096 KB |
Output is correct |
30 |
Correct |
170 ms |
158084 KB |
Output is correct |
31 |
Correct |
173 ms |
158148 KB |
Output is correct |
32 |
Correct |
205 ms |
158180 KB |
Output is correct |
33 |
Correct |
200 ms |
158272 KB |
Output is correct |
34 |
Correct |
204 ms |
158156 KB |
Output is correct |
35 |
Correct |
196 ms |
158104 KB |
Output is correct |
36 |
Correct |
201 ms |
158128 KB |
Output is correct |