#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include <stdio.h>
#include <string.h>
#include<iostream>
using namespace std;
char map_[55][55];
char directs[2010][10];
int total_directs;
bool flag[55][55][2010];
struct hereT{
int x,y,cen;
};
int height,width;
queue<hereT>que;
void push_eve(hereT temp){
hereT haha;
//cout<<"posi:"<<temp.x<<" "<<temp.y<<"direct:"<<directs[temp.cen]<<endl;
if(flag[temp.x][temp.y][temp.cen]==1)return;
if(temp.cen>total_directs){map_[temp.x][temp.y]='*';return;}
if(directs[temp.cen][0]=='N'){
for(int i=1;;i++){
int newx=temp.x-i,newy=temp.y;
if(newx<1)return;
if(map_[newx][newy]=='X')return;
haha.cen = temp.cen+1;
haha.x=newx;haha.y=newy;
que.push(haha);
flag[temp.x][temp.y][temp.cen]=1;
}
}
else if(directs[temp.cen][0]=='S'){
for(int i=1;;i++){
int newx=temp.x+i,newy=temp.y;
if(newx>height)return;
if(map_[newx][newy]=='X')return;
haha.cen = temp.cen+1;
haha.x=newx;haha.y=newy;
que.push(haha);
flag[temp.x][temp.y][temp.cen]=1;
}
}
else if(directs[temp.cen][0]=='W'){
for(int i=1;;i++){
int newx=temp.x,newy=temp.y-i;
if(newy<1)return;
if(map_[newx][newy]=='X')return;
haha.cen = temp.cen+1;
haha.x=newx;haha.y=newy;
que.push(haha);
flag[temp.x][temp.y][temp.cen]=1;
}
}
else if(directs[temp.cen][0]=='E'){
for(int i=1;;i++){
int newx=temp.x,newy=temp.y+i;
if(newy>width)return;
if(map_[newx][newy]=='X')return;
haha.cen = temp.cen+1;
haha.x=newx;haha.y=newy;
que.push(haha);
flag[temp.x][temp.y][temp.cen]=1;
}
}
}
int main()
{
memset(flag,0,sizeof(flag));
cin>>height>>width;
hereT start;
for(int i=1;i<=height;i++){
for(int j=1;j<=width;j++){
cin>>map_[i][j];
if(map_[i][j]=='*'){start.x=i;start.y=j;start.cen=1;}
}
}
/*
for(int i=1;i<=height;i++){
for(int j=1;j<=width;j++){
cout<<map_[i][j];
}cout<<endl;
}*/
cin>>total_directs;
map_[start.x][start.y]='.';
for(int i=1;i<=total_directs;i++)cin>>directs[i];
que.push(start);
while(!que.empty()){
start=que.front();
que.pop();
push_eve(start);
}
for(int i=1;i<=height;i++){
for(int j=1;j<=width;j++){
cout<<map_[i][j];
}cout<<endl;
}
}
BFS 逃跑的拉尔夫
最新推荐文章于 2022-03-24 09:01:19 发布