#include<bits/stdc++.h>
using namespace std;
const int N = 5 * 1e2 + 5;
const int inf = 1000000000;
#define int long long
#define F first
#define S second
#define pb push_back
#define ppb pop_back
int r, c, m, ans;
bool block[N][N], ok[N][N][N], used[N][N][N];
char s[5005];
void rec(int x, int y, int step){
used[x][y][step] = 1;
if(step == 0){
ok[x][y][step] = 1;
return;
}
if(s[step] == '?'){
if(!block[x + 1][y] && !used[x + 1][y][step - 1]) rec(x + 1, y, step - 1);
if(ok[x + 1][y][step - 1]) ok[x][y][step] = 1;
if(!block[x - 1][y] && !used[x - 1][y][step - 1]) rec(x - 1, y, step - 1);
if(ok[x - 1][y][step - 1]) ok[x][y][step] = 1;
if(!block[x][y + 1] && !used[x][y + 1][step - 1]) rec(x, y + 1, step - 1);
if(ok[x][y + 1][step - 1]) ok[x][y][step] = 1;
if(!block[x][y - 1] && !used[x][y - 1][step - 1]) rec(x, y - 1, step - 1);
if(ok[x][y - 1][step - 1]) ok[x][y][step] = 1;
}
else if(s[step] == 'N'){
if(!block[x + 1][y] && !used[x + 1][y][step - 1]) rec(x + 1, y, step - 1);
if(ok[x + 1][y][step - 1]) ok[x][y][step] = 1;
}
else if(s[step] == 'S'){
if(!block[x - 1][y] && !used[x - 1][y][step - 1]) rec(x - 1, y, step - 1);
if(ok[x - 1][y][step - 1]) ok[x][y][step] = 1;
}
else if(s[step] == 'W'){
if(!block[x][y + 1] && !used[x][y + 1][step - 1]) rec(x, y + 1, step - 1);
if(ok[x][y + 1][step - 1]) ok[x][y][step] = 1;
}
else{
if(!block[x][y - 1] && !used[x][y - 1][step - 1]) rec(x, y - 1, step - 1);
if(ok[x][y - 1][step - 1]) ok[x][y][step] = 1;
}
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> r >> c >> m;
for(int i = 1; i <= r; ++i){
block[i][0] = 1;
block[i][c + 1] = 1;
for(int j = 1; j <= c; ++j){
char c1;
cin >> c1;
if(c1 == '#') block[i][j] = 1;
// cout << block[i][j];
}
// cout << '\n';
}
for(int j = 1; j <= c; ++j){
block[0][j] = 1;
block[r + 1][j] = 1;
}
for(int i = 1; i <= m; ++i){
cin >> s[i];
}
for(int i = 1; i <= r; ++i){
for(int j = 1; j <= c; ++j){
if(!block[i][j]){
// cout << i << ' ' << j << '\n';
rec(i, j, m);
if(ok[i][j][m]) ans++;
}
}
}
cout << ans;
return 0;
}
Compilation message
nautilus.cpp:53:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
53 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
10956 KB |
Output is correct |
2 |
Correct |
5 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5608 KB |
Output is correct |
5 |
Correct |
3 ms |
4684 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
10956 KB |
Output is correct |
2 |
Correct |
5 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5608 KB |
Output is correct |
5 |
Correct |
3 ms |
4684 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
20 ms |
11060 KB |
Output is correct |
8 |
Correct |
9 ms |
9420 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
4684 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
28 ms |
11012 KB |
Output is correct |
13 |
Correct |
28 ms |
10892 KB |
Output is correct |
14 |
Correct |
21 ms |
10704 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
27 ms |
11020 KB |
Output is correct |
18 |
Correct |
31 ms |
10988 KB |
Output is correct |
19 |
Correct |
12 ms |
10268 KB |
Output is correct |
20 |
Correct |
8 ms |
7500 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
10956 KB |
Output is correct |
2 |
Correct |
5 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5608 KB |
Output is correct |
5 |
Correct |
3 ms |
4684 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
20 ms |
11060 KB |
Output is correct |
8 |
Correct |
9 ms |
9420 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
4684 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
28 ms |
11012 KB |
Output is correct |
13 |
Correct |
28 ms |
10892 KB |
Output is correct |
14 |
Correct |
21 ms |
10704 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
27 ms |
11020 KB |
Output is correct |
18 |
Correct |
31 ms |
10988 KB |
Output is correct |
19 |
Correct |
12 ms |
10268 KB |
Output is correct |
20 |
Correct |
8 ms |
7500 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Execution timed out |
1089 ms |
173288 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |