样例没过
查看原帖
样例没过
955954
Miracle_InDream楼主2023/12/15 22:17
#include<bits/stdc++.h>
using namespace std;
int w,h;
char mp[25][25];
bool vis[25][25];
int maxx=0;
struct node
{
	int x;
	int y;
	int step;
};
node s,t;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
queue<node> q;
void bfs()
{
	s.step=1;
	vis[s.x][s.y]=1;
	q.push(s);
	while(!q.empty())
	{
		node tmp=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			node a;
			a.x=tmp.x+dx[i];
			a.y=tmp.y+dy[i];
			if(a.x>0&&a.y>0&&a.x<=w&&a.y<=h&&mp[a.x][a.y]!='#'&&vis[a.x][a.y]==0)
			{
				a.step=tmp.step+1;
				vis[a.x][a.y]=1;
				q.push(a);
				maxx=max(a.step,maxx);
			}
		}
	}
}
int main()
{
	cin>>w>>h;
	for(int i=1;i<=w;i++)
	{
		for(int j=1;j<=h;j++)
		{
			cin>>mp[i][j];
			if(mp[i][j]=='@')
			{
				s.x=i;
				s.y=j;
			}
		}
	}
	bfs();
	cout<<maxx;
	return 0;
}
2023/12/15 22:17
加载中...