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>
#include <bitset>
#include <map>
using namespace std;
char g[501][501];
int n,m,M;
string s;
map<char,pair<int,int>> dir;
bitset<5001> pos[501][501];
bitset<5001> fx[501][501];
bool dfs(int x,int y,int ind)
{
if(x<0 or y<0 or x>=n or y>=m or g[x][y]=='#')
return 0;
if(fx[x][y][ind])
return pos[x][y][ind];
if(ind==M)
{
pos[x][y][ind]=1;
fx[x][y][ind]=1;
return 1;
}
if(s[ind]=='?')
{
fx[x][y][ind]=1;
for(auto lp:dir)
if(dfs(x+lp.second.first,y+lp.second.second,ind+1))
pos[x][y][ind]=1;
}
else
{
fx[x][y][ind]=1;
pos[x][y][ind]=dfs(x+dir[s[ind]].first,y+dir[s[ind]].second,ind+1);
}
return pos[x][y][ind];
}
int main()
{
dir['N']={-1,0};
dir['S']={1,0};
dir['E']={0,1};
dir['W']={0,-1};
cin>>n>>m>>M;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>g[i][j];
cin>>s;
int cnt=0;
for(int x=0;x<n;x++)
for(int y=0;y<m;y++)
dfs(x,y,0);
for(int x=0;x<n;x++)
for(int y=0;y<m;y++)
cnt+=pos[x][y][M];
cout<<cnt<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |