广搜+判重,不判重会MLE
三维判重,vis[I][j][k]表示在第(I,j)个点处理第k个方向,注意不要错判
#include<iostream>
#include<queue>
#include<string>
#include<utility>
#include<cstring>
using namespace std;
int r,c,n;
char G[52][52];
pair<int,int>start;
const int mx[4]={-1,0,1,0 };
const int my[4]={0 ,1,0,-1};
queue<pair<int,int> >q;
bool vis[52][52][1048];
void init(void){
cin>>r>>c;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++){
cin>>G[i][j];
if(G[i][j]=='*'){
start=make_pair(i,j);
G[i][j]='.';
}
}
memset(vis,0,sizeof(vis));
}
void print(void){
pair<int,int>p;
while(!q.empty()){
p=q.front();q.pop();
G[p.first][p.second]='*';
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<G[i][j];
}
cout<<endl;
}
}
inline int check(pair<int,int> t){
if(t.first<0||t.first>=r||t.second<0||t.second>=c)return 0;
if(G[t.first][t.second]=='X')return 0;
return 1;
}
int BFS(int d){
int s=q.size();
pair<int,int> now;
while(s--){
now=q.front();q.pop();
while(1){
now=make_pair(now.first+mx[d],now.second+my[d]);
if(check(now)){
if(vis[now.first][now.second][n])continue;
q.push(now);
vis[now.first][now.second][n]=1;
}
else break;
}
}
}
void solve(void){
cin>>n;
string temp;int d;
q.push(start);
while(n--){
cin>>temp;
if(temp=="NORTH") d=0;
else if(temp=="EAST")d=1;
else if(temp=="SOUTH")d=2;
else if(temp=="WEST")d=3;
BFS(d);
}
print();
}
int main(){
ios::sync_with_stdio(false);
init();
solve();
return 0;
}