20分求调
  • 板块P1189 SEARCH
  • 楼主hema5177
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/6/22 10:29
  • 上次更新2025/6/22 14:57:05
查看原帖
20分求调
648623
hema5177楼主2025/6/22 10:29

思路是bfs,找到车的位置,然后存进队列,在输入方向的时候将每个方向可能的位置存进队列,最后将还在队列的位置标记为*

不知道哪里错了,第二个测试点总输出乱码…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<deque>
using namespace std;
int n,m,k,x[1001],y[1001];
char a[51][51];
string s;
deque<pair<int,int> > q;
bool check(int i,int j)
{
	return i>=1&&j>=1&&i<=n&&j<=m&&a[i][j]!='X';
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
			if(a[i][j]=='*')
			{
				q.push_back({i,j});
				a[i][j]='.';
			}
		}
	}
	cin>>k;
	while(k--)
	{
		cin>>s;
		int l=0;
		memset(x,0,sizeof(x));
		memset(y,0,sizeof(y));
		while(!q.empty())
		{
			x[++l]=q.front().first;
			y[l]=q.front().second;
			q.pop_front();
		}
		for(int i=1;i<=l;i++)
		{
			if(s=="NORTH")
			{
				while(check(x[i]-1,y[i]))
				{
					x[i]--;
					q.push_back({x[i],y[i]});
				}
			}
			if(s=="SOUTH")
			{
				while(check(x[i]+1,y[i]))
				{
					x[i]++;
					q.push_back({x[i],y[i]});
				}
			}
			if(s=="EAST")
			{
				while(check(x[i],y[i]+1))
				{
					y[i]++;
					q.push_back({x[i],y[i]});
				}
			}
			if(s=="WEST")
			{
				while(check(x[i],y[i]-1))
				{
					y[i]--;
					q.push_back({x[i],y[i]});
				}
			}
		}
	}
	while(!q.empty())
	{
		a[q.front().first][q.front().second]='*';
		q.pop_front();
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++) cout<<a[i][j];
		cout<<endl;
	}
}
2025/6/22 10:29
加载中...