This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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[2][505][505];
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[curr_pos][i][j] = 0;
continue;
}
if (pos==0) {
dp[curr_pos][i][j] = 1;
continue;
}
dp[curr_pos][i][j] = 0;
switch (moves[pos-1]) {
case 'N':
dp[curr_pos][i][j] = ((in_mat(i+1,j)) ? dp[last_pos][i+1][j] : 0);
break;
case 'S':
dp[curr_pos][i][j] = ((in_mat(i-1,j)) ? dp[last_pos][i-1][j] : 0);
break;
case 'E':
dp[curr_pos][i][j] = ((in_mat(i,j-1)) ? dp[last_pos][i][j-1] : 0);
break;
case 'W':
dp[curr_pos][i][j] = ((in_mat(i,j+1)) ? dp[last_pos][i][j+1] : 0);
break;
default:
for (int k = 0; k < 4; k++) {
int iv = i+dx[k];
int jv = j+dy[k];
dp[curr_pos][i][j] |= ((in_mat(iv,jv)) ? dp[last_pos][iv][jv] : 0);
}
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 < p; i++) {
cnt += (moves[i]!='*');
}
if (x>100&y>100&&(double)cnt/p<=0.003) {
int ans = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
ans += (arr[i][j]!='#');
}
}
std::cout << ans << "\n";
return 0;
}
solve_dp();
int ret = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
ret += dp[p&1][i][j];
}
}
std::cout << ret << "\n";
}
Compilation message (stderr)
nautilus.cpp: In function 'int main()':
nautilus.cpp:82:7: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
82 | if (x>100&y>100&&(double)cnt/p<=0.003) {
| ~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |