路过的大佬能不能在我的代码上改一下再加一点注释。
急急急
#include<bits/stdc++.h>
using namespace std;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int n,m,ans,x,y; char c;
bool mp[1010][1010],flag[1010][1010];
int ansxy[1010][1010];
void BFS(){
queue<int> qx,qy;
qx.push(x); qy.push(y);
while(!qx.empty()){
int nx = qx.front();
int ny = qy.front();
qx.pop(); qy.pop();
for(int i = 0;i < 4;i++){
int tx = nx + dx[i];
int ty = ny + dy[i];
if(tx < 1 || tx > n || ty < 1 || ty > n
|| flag[tx][ty]) continue;
if(mp[nx][ny] == mp[tx][ty]) continue;
flag[tx][ty] = 1;
qx.push(tx); qy.push(ty);
}
} return ;
} int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
ansxy[i][j] = -1;
cin >> c;
mp[i][j] = c - '0';
} while(m--){
cin >> x >> y;
if(ansxy[x][y] != -1){
cout << ansxy[x][y] << "\n";
continue;
} for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
flag[i][j] = 0;
} flag[x][y] = 1;
BFS(); ans = 0;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
ans += flag[i][j];
} cout << ans << "\n";
ansxy[x][y] = ans;
} return 0;
}