#include<bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
char chess[1005][1005], mark[1005][1005];
int n, m, x, y, ans=1, sum, re[1005][1005],u;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
typedef pair<int, int> pll;
queue<pll> q;
int check(int a, int b) {
return mark[a][b];
}
int bfs(int x, int y)
{
u = ans;
q.push({ x, y });
while (q.size()) {
auto t = q.front();
//if (re[x][y] != -1)return re[x][y];
mark[x][y] = chess[x][y];
q.pop();
for (int i = 0; i < 4; i++) {
int a = t.first + dx[i], b = t.second + dy[i];
if (a <= n && b <= n && a > 0 && b > 0) {
if (chess[a][b] != chess[t.first][t.second]) {
if (check(a, b) == -1) {
mark[a][b] = chess[a][b];
ans++;
//re[a][b] = ans;
q.push({ a,b });
chess[x][y] = chess[a][b];
}
}
}
}
}
if (u == ans) {
return u;
}
else return ans-u+1;
}
int main()
{
memset(mark, -1, sizeof mark);
//memset(re, -1, sizeof(re));
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> chess[i][j];
}
}
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
cout << bfs(x,y);
if (i != m) {
cout << endl;
}
}
}
https://www.luogu.com.cn/problem/P1141
只有样例过了。但是不知道为什么错了改了好久了