题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
using namespace std;
const int N = 1010;
int n, m;
char str[N][N];
void bfs(int x, int y)
{
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
queue<pair<int, int>> q;
q.push({x, y});
str[x][y] = '.';
while(q.size())
{
auto t = q.front();
q.pop();
int x = t.first, y = t.second;
for (int i = 0; i < 8; i ++)
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && str[a][b] == 'W')
{
q.push({a, b});
str[a][b] = '.';
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++) cin >> str[i];
int res = 0;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
if (str[i][j] == 'W')
{
res ++;
bfs(i, j);
}
cout << res;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla