还是有两个点TLE了,求助大佬改怎么改
查看原帖
还是有两个点TLE了,求助大佬改怎么改
890929
yiban202241040105楼主2023/10/18 00:02
#include<bits/stdc++.h>
using namespace std;
#define read(a) {char c;while((c=getchar())>47) a=a*10+(c^48);}
const int N = 100000;
int n, m;
int front = 0, rear = 0;
int q[N];
char s[101][101];
void push(int x, int y)
{
	rear = (rear + 1) % N;
	q[rear] = x;
	rear = (rear + 1) % N;
	q[rear] = y;
}
int first()
{
	front = (front + 1) % N;
	return q[front];
}
void bfs(int x, int y)
{
	s[x][y] = '.';
	if (x < 0 || x >= n || y < 0 || y >= m)
		return;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
			if (s[x + i][y + j] == 'W')
			{
				push(x + i, y + j);
			}
		}
	}
}
int main() {
	memset(q, 0, sizeof(q));
	read(n);
	read(m);
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%s", s[i]);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (s[i][j] == 'W')
			{
				push(i, j);
				while (front != rear)
				{
					int x = first();
					int y = first();
					bfs(x, y);
				}
				ans++;
			}

		}
	}
	printf("%d", ans);
	return 0;
}
2023/10/18 00:02
加载中...