有3,10,11点会TLE
这是我的代码,问问巨佬们该怎么改orzOTZ
#include <bits/stdc++.h>
using namespace std;
int vis[1010][1010];
// int f[1010][1010];
int a[1010][1010];
int x, y;
char c;
int cnt;
int n, m;
int sum[1010][1010];
int dx[] = {0, 1, 0, -1, 0};
int dy[] = {0, 0, 1, 0, -1};
void bfs(int aa, int bb)
{
queue<pair<int, int>> q;
pair<int, int> t;
q.push({aa, bb});
vis[aa][bb] = 1;
while (!q.empty())
{
t = q.front();
q.pop();
for (int i = 1; i <= 4; i++)
{
int nx = t.first + dx[i];
int ny = t.second + dy[i];
if (nx<1 || ny<1 || nx>n || ny>n || vis[nx][ny]==1 || a[nx][ny]==a[t.first][t.second])
continue;
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> c;
a[i][j]=int(c-'0');
// cout<<a[i][j];
}
// cout<<endl;
}
while (m--)
{
cin >> x >> y;
if(sum[x][y])
{
cout<<sum[x][y]<<endl;
continue;
}
memset(vis, 0, sizeof(vis));
bfs(x, y);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (vis[i][j])
{
cnt++;
// cout<<i<<' '<<j<<endl;
}
}
}
sum[x][y]=cnt;
cout << cnt << endl;
cnt = 0;
}
return 0;
}