广搜绿题 求助!!!
  • 板块学术版
  • 楼主CSP_zyh
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/7/21 23:19
  • 上次更新2023/11/3 08:20:21
查看原帖
广搜绿题 求助!!!
549886
CSP_zyh楼主2023/7/21 23:19

传送门

#include<bits/stdc++.h>
using namespace std;
int r,c;
int head,tail;
int n,sx,sy;
char f[55][55];
string s[1005];
struct node{
	int x,y,now;
}que[9000000];
//           下 右 上 左 
int stepx[4]={1,0,-1,0};
int stepy[4]={0,1,0,-1}; 
void bfs(){
	que[tail++]={sx,sy,1};
	while(head<tail){
		if(que[head].now==n+1){
			f[que[head].x][que[head].y]='*';
			head++;
			continue;
		}
		string way=s[que[head].now];
		int t; 
		if(way=="NORTH")	t=2;
		else if(way=="SOUTH")	t=0;
		else if(way=="EAST")	t=1;
		else t=3;
		for(int i=0;i<max(r,c);i++){
			int xx=stepx[t]+que[head].x;
			int yy=stepy[t]+que[head].y;
			if(t==0){
				xx+=i;
			}
			else if(t==1){
				yy+=i;
			}
			else if(t==2){
				xx-=i;
			}
			else{
				yy-=i;
			}
			if(xx>=1&&xx<=r&&yy>=1&&yy<=c&&f[xx][yy]!='X'){
				que[tail++]={xx,yy,que[head].now+1};
			}
			else{
				break;
			}
		}
		head++;
	}
} 
int main(){
	cin>>r>>c;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			cin>>f[i][j];
			if(f[i][j]=='*'){
				sx=i;
				sy=j;
				f[i][j]='.';
			}
		}
	}
	cout<<sx<<" "<<sy<<endl; 
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];
	}
	bfs();
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			cout<<f[i][j];
		}
		cout<<endl;
	}
	return 0;
}
2023/7/21 23:19
加载中...