离谱,我的BFS怎么卡在20%,明明O(n)的
#米哈游##笔试##3.19##悬赏#
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <unordered_map>
#include <string>
using namespace std;
int getcount(int n, int m, vector<vector<char>> nums1)
{
vector<vector<int>> visited(n, vector<int>(m, 0));
deque<pair<int, int>> q;
vector<vector<int>> delta = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int cnt = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (visited[i][j] != 0)
continue;
q.push_back({i, j});
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop_front();
visited[x][y] = 1;
for (auto &add : delta)
{
int nx = x + add[0];
int ny = y + add[1];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && nums1[nx][ny] == nums1[x][y] && visited[nx][ny] == 0)
{
q.push_back({nx, ny});
}
}
}
cnt++;
}
}
return cnt;
}
int main()
{
int n, m;
cin >> n >> m;
vector<vector<char>> nums1(n, vector<char>(m));
vector<vector<char>> nums2(n, vector<char>(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
char c;
cin >> c;
nums1[i][j] = c;
if (c == 'B')
nums2[i][j] = 'G';
else
nums2[i][j] = c;
}
}
int cnt1 = getcount(n, m, nums1);
int cnt2 = getcount(n, m, nums2);
cout << cnt1 - cnt2 << endl;
}