大佬们帮帮蒟蒻吧QWQ,感觉自己写的没错哇:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define QWQ cin.tie(0)->sync_with_stdio(false);
const int maxn = 1111;
char g[maxn][maxn];
bool st[maxn][maxn];
int n, m, i, j, sum = 0;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
queue< pair<int , int > > q;
int bfs(){
int xx, yy;
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
xx = dx[i] + x;
yy = dy[i] + y;
if(!st[xx][yy] and ((g[x][y] == '0' and g[xx][yy] == '1') or (g[x][y] == '1' and g[xx][yy] == '0'))
and xx >= 1 and xx <= n and yy >= 1 and yy <= n){
st[xx][yy] = true;
++sum;
q.push(make_pair(xx, yy));
}
}
}
return sum;
}
signed main(){
QWQ
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
cin >> g[i][j];
}
while(m--){
cin >> i >> j;
q.push(make_pair(i, j));
int res = bfs();
cout << res << endl;
while(!q.empty()) q.pop();
memset(st, 0, sizeof st);
sum = 0;
}
return 0;
}