我爬了,应该是读入的问题
#include <cstdio>
int n, m;
int mapp[1002][1002];
int ans[1002][1002];
bool vis[1002][1002];
bool vis2[1002][1002];
int dfs(int x, int y)
{
if (x <= 0 || x >= n + 1 || y <= 0 || y >= n + 1)
return 0;
if (vis[x][y])
return 0;
vis[x][y] = true;
int ans = 1;
if (mapp[x + 1][y] == mapp[x][y] ^ 1)
ans += dfs(x + 1, y);
if (mapp[x - 1][y] == mapp[x][y] ^ 1)
ans += dfs(x - 1, y);
if (mapp[x][y + 1] == mapp[x][y] ^ 1)
ans += dfs(x, y + 1);
if (mapp[x][y - 1] == mapp[x][y] ^ 1)
ans += dfs(x, y - 1);
return ans;
}
void print(int x, int y, int k)
{
if (x <= 0 || x >= n + 1 || y <= 0 || y >= n + 1)
return;
if (vis2[x][y])
return;
vis2[x][y] = true;
ans[x][y] = k;
if (mapp[x + 1][y] == mapp[x][y] ^ 1)
print(x + 1, y, k);
if (mapp[x - 1][y] == mapp[x][y] ^ 1)
print(x - 1, y, k);
if (mapp[x][y + 1] == mapp[x][y] ^ 1)
print(x, y + 1, k);
if (mapp[x][y - 1] == mapp[x][y] ^ 1)
print(x, y - 1, k);
}
int main()
{
scanf("%d%d", &n, &m);
int i, j;
int x, y;
char ch;
for (i = 0; i <= n + 1; i++)
for (j = 0; j <= n + 1; j++)
mapp[i][j] = -1;
getchar();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
ch = getchar();
mapp[i][j] = ch == '0' ? 0 : 1;
}
getchar();
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (!vis[i][j])
print(i, j, dfs(i, j));
for (i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
printf("%d\n", ans[x][y]);
}
return 0;
}