This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
nautilus.cpp:53:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
53 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |