bfs MLE 80pts求助
查看原帖
bfs MLE 80pts求助
917372
hanxinyao楼主2025/1/19 16:37
#include <bits/stdc++.h>
#define int long long

using namespace std;

int n,m,cnt;
int mp[110][110],f[110][110];
int mov[8][2] = {{0,1},{0,-1},{1,1},{1,0},{1,-1},{-1,1},{-1,0},{-1,-1}};
char tmp;

void bfs(int x,int y)
{
	queue<pair<int,int> > q;
	q.push({x,y});
	while (!q.empty())
	{
		int nx = q.front().first,ny = q.front().second;
		q.pop();
		f[nx][ny] = 2;
		for (int i = 0;i < 8;i++)
		{
			int xx = nx + mov[i][0],yy = ny + mov[i][1];
			if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && f[xx][yy] == 1)
			{
				q.push({xx,yy});
			}
		}
	}
}
signed main()
{
	cin >> n >> m;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			cin >> tmp;
			mp[i][j] = f[i][j] = (tmp == 'W' ? 1 : 0);
		}
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			if (f[i][j] == 1)
			{
				bfs(i,j);
				cnt++;
			}
		}
	}
	cout << cnt;
	return 0;
 } 

交上去MLE两个点

2025/1/19 16:37
加载中...