#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int n,m,s;
int lasti,lastj;
int count(vector<char> &ins,vector<vector<char> > &a,int k,int i,int j,int dir){
if(i<0||i==n||j<0||j==m||a[i][j]=='#')return count(ins,a,k,lasti,lastj,dir);
int ans=0;
if(a[i][j]=='*'){
ans=1;
a[i][j]='.';
}
if(k==s)return ans;
lasti=i,lastj=j;
switch(ins[k]){
case 'F': {
if(dir==0)return ans+count(ins,a,k+1,i-1,j,dir);
else if(dir==1)return ans+count(ins,a,k+1,i,j+1,dir);
else if(dir==2)return ans+count(ins,a,k+1,i+1,j,dir);
else return ans+count(ins,a,k+1,i,j-1,dir);
break;
}
case 'D': {
return ans+count(ins,a,k+1,i,j,(dir+1)%4);
break;
}
case 'E':{
return ans+count(ins,a,k+1,i,j,(dir-1+4)%4);
}
}
return 0;
}
int main(){
while(cin>>n>>m>>s){
if(n==0&&m==0&&s==0)return 0;
char cdir;
int dir,x,y;
vector<vector<char> > a(n,vector<char>(m));
vector<char> ins(s);
string S;
for(int i=0;i<n;i++){
ws(cin);
getline(cin,S);
for(int j=0;j<m;j++)
{
a[i][j]=S[j];
if(S[j]=='N'||S[j]=='S'||S[j]=='L'||S[j]=='O'){
cdir=S[j];
x=i;
y=j;
}
}
}
ws(cin);
getline(cin,S);
for(int i=0;i<s;i++)
ins[i]=S[i];
switch(cdir){
case 'N' : dir=0;break;
case 'L' : dir=1;break;
case 'S' : dir=2;break;
case 'O' : dir=3;
}
cout<<count(ins,a,0,x,y,dir)<<endl;
}
}
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int n,m,s;
int lasti,lastj;
int count(vector<char> &ins,vector<vector<char> > &a,int k,int i,int j,int dir){
if(i<0||i==n||j<0||j==m||a[i][j]=='#')return count(ins,a,k,lasti,lastj,dir);
int ans=0;
if(a[i][j]=='*'){
ans=1;
a[i][j]='.';
}
if(k==s)return ans;
lasti=i,lastj=j;
switch(ins[k]){
case 'F': {
if(dir==0)return ans+count(ins,a,k+1,i-1,j,dir);
else if(dir==1)return ans+count(ins,a,k+1,i,j+1,dir);
else if(dir==2)return ans+count(ins,a,k+1,i+1,j,dir);
else return ans+count(ins,a,k+1,i,j-1,dir);
break;
}
case 'D': {
return ans+count(ins,a,k+1,i,j,(dir+1)%4);
break;
}
case 'E':{
return ans+count(ins,a,k+1,i,j,(dir-1+4)%4);
}
}
return 0;
}
int main(){
while(cin>>n>>m>>s){
if(n==0&&m==0&&s==0)return 0;
char cdir;
int dir,x,y;
vector<vector<char> > a(n,vector<char>(m));
vector<char> ins(s);
string S;
for(int i=0;i<n;i++){
ws(cin);
getline(cin,S);
for(int j=0;j<m;j++)
{
a[i][j]=S[j];
if(S[j]=='N'||S[j]=='S'||S[j]=='L'||S[j]=='O'){
cdir=S[j];
x=i;
y=j;
}
}
}
ws(cin);
getline(cin,S);
for(int i=0;i<s;i++)
ins[i]=S[i];
switch(cdir){
case 'N' : dir=0;break;
case 'L' : dir=1;break;
case 'S' : dir=2;break;
case 'O' : dir=3;
}
cout<<count(ins,a,0,x,y,dir)<<endl;
}
}
Comments
Post a Comment