#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi2,popcnt,lzcnt")
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <cstring>
int x, y, p;
char arr[505][505];
char moves[5005];
int dp[505][505][2];
const int dx[] = {0,1,0,-1};
const int dy[] = {1,0,-1,0};
inline int in_mat(int i, int j) {
return !(i<0||i>=x||j<0||j>=y);
}
void solve_dp() {
for (int pos = 0; pos <= p; pos++) {
int curr_pos = pos&1;
int last_pos = curr_pos^1;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (arr[i][j]=='#') {
dp[i][j][curr_pos] = 0;
continue;
}
if (pos==0) {
dp[i][j][curr_pos] = 1;
continue;
}
dp[i][j][curr_pos] = 0;
switch (moves[pos-1]) {
case 'N':
dp[i][j][curr_pos] = ((in_mat(i+1,j)) ? dp[i+1][j][last_pos] : 0);
break;
case 'S':
dp[i][j][curr_pos] = ((in_mat(i-1,j)) ? dp[i-1][j][last_pos] : 0);
break;
case 'E':
dp[i][j][curr_pos] = ((in_mat(i,j-1)) ? dp[i][j-1][last_pos] : 0);
break;
case 'W':
dp[i][j][curr_pos] = ((in_mat(i,j+1)) ? dp[i][j+1][last_pos] : 0);
break;
default:
for (int k = 0; k < 4; k++) {
int iv = i+dx[k];
int jv = j+dy[k];
dp[i][j][curr_pos] |= ((in_mat(iv,jv)) ? dp[iv][jv][last_pos] : 0);
if (dp[i][j][curr_pos]) {
break;
}
}
break;
}
}
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> x >> y >> p;
for (int i = 0; i < x; i++) {
std::cin >> arr[i];
}
std::cin >> moves;
int cnt = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
cnt += (arr[i][j]=='#');
}
}
solve_dp();
int ret = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
ret += dp[i][j][p&1];
}
}
std::cout << ret << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2640 KB |
Output is correct |
4 |
Correct |
5 ms |
2640 KB |
Output is correct |
5 |
Correct |
3 ms |
2640 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2640 KB |
Output is correct |
4 |
Correct |
5 ms |
2640 KB |
Output is correct |
5 |
Correct |
3 ms |
2640 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2640 KB |
Output is correct |
8 |
Correct |
6 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2640 KB |
Output is correct |
10 |
Correct |
4 ms |
2640 KB |
Output is correct |
11 |
Correct |
3 ms |
2640 KB |
Output is correct |
12 |
Correct |
3 ms |
2640 KB |
Output is correct |
13 |
Correct |
6 ms |
2640 KB |
Output is correct |
14 |
Correct |
7 ms |
2808 KB |
Output is correct |
15 |
Correct |
4 ms |
2640 KB |
Output is correct |
16 |
Correct |
3 ms |
2640 KB |
Output is correct |
17 |
Correct |
3 ms |
2640 KB |
Output is correct |
18 |
Correct |
3 ms |
2640 KB |
Output is correct |
19 |
Correct |
3 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2640 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2640 KB |
Output is correct |
4 |
Correct |
5 ms |
2640 KB |
Output is correct |
5 |
Correct |
3 ms |
2640 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2640 KB |
Output is correct |
8 |
Correct |
6 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2640 KB |
Output is correct |
10 |
Correct |
4 ms |
2640 KB |
Output is correct |
11 |
Correct |
3 ms |
2640 KB |
Output is correct |
12 |
Correct |
3 ms |
2640 KB |
Output is correct |
13 |
Correct |
6 ms |
2640 KB |
Output is correct |
14 |
Correct |
7 ms |
2808 KB |
Output is correct |
15 |
Correct |
4 ms |
2640 KB |
Output is correct |
16 |
Correct |
3 ms |
2640 KB |
Output is correct |
17 |
Correct |
3 ms |
2640 KB |
Output is correct |
18 |
Correct |
3 ms |
2640 KB |
Output is correct |
19 |
Correct |
3 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2640 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Execution timed out |
1068 ms |
2640 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |