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 <iostream>
using namespace std;
const int MAX_GRID = 505, MAX_M = 5005;
int r, c, m;
bool grid[MAX_GRID][MAX_GRID], visited[MAX_GRID][MAX_GRID][MAX_M], position[MAX_GRID][MAX_GRID];
string input;
void solve(int x, int y, int i) {
if (x < 0 || x >= c || y < 0 || y >= r || !grid[y][x] || visited[y][x][i]) {
return;
}
visited[y][x][i] = true;
if (i == input.length()) {
position[y][x] = true;
return;
}
switch (input[i]) {
case 'N':
solve(x, y - 1, i + 1);
break;
case 'E':
solve(x + 1, y, i + 1);
break;
case 'S':
solve(x, y + 1, i + 1);
break;
case 'W':
solve(x - 1, y, i + 1);
break;
case '?':
solve(x, y - 1, i + 1);
solve(x + 1, y, i + 1);
solve(x, y + 1, i + 1);
solve(x - 1, y, i + 1);
break;
}
}
int main() {
cin >> r >> c >> m;
for (int i = 0; i < r; ++i) {
string s;
cin >> s;
for (int j = 0; j < c; ++j) {
grid[i][j] = (s[j] == '.');
}
}
cin >> input;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
solve(j, i, 0);
}
}
/* bool dp[MAX_GRID][MAX_GRID][MAX_M];
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
for (int pos = 0; pos < input.length(); ++pos) {
if (!grid[j][i]) {
continue;
}
if (pos == input.length()) {
position[j][i] = true;
continue;
}
switch (input[pos]) {
case 'N':
solve(i, j - 1, pos + 1);
break;
case 'E':
solve(i + 1, j, pos + 1);
break;
case 'S':
solve(i, j + 1, pos + 1);
break;
case 'W':
solve(i - 1, j, pos + 1);
break;
case '?':
solve(i, j - 1, pos + 1);
solve(i + 1, j, pos + 1);
solve(i, j + 1, pos + 1);
solve(i - 1, j, pos + 1);
break;
}
}
}
} */
int result = 0;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
result += position[i][j];
}
}
cout << result;
}
Compilation message (stderr)
nautilus.cpp: In function 'void solve(int, int, int)':
nautilus.cpp:16:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if (i == input.length()) {
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |