Debiks @ 2024-11-06 00:50:13
#include <bits/stdc++.h>
using namespace std;
int n, m, v[1005][1005];
char a[1005][1005];
int cn[100005];
void dfs(int x, int y, int num, int p) {
if (x == 0 || y == 0 || x == n + 1 || y == n + 1 || v[x][y] == 1 || num == a[x][y]) return;
if (!v[x][y]) {
v[x][y] = 1;
cn[p]++;
dfs(x + 1, y, a[x][y], p);
dfs(x, y + 1, a[x][y], p);
dfs(x - 1, y, a[x][y], p);
dfs(x, y - 1, a[x][y], p);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
int x1, y1;
for (int i = 0; i < m; i++) {
cin >> x1 >> y1;
dfs(x1, y1, a[x1][y1] - '0', i);
cout << cn[i] << endl;
memset(v, 0, sizeof(v));
}
return 0;
}
by masonxiong @ 2024-11-06 07:44:22
@Debiks 请您预处理。