输出输入较大,注意用scanf,printf,否则可能超时
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
//#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <string>
using namespace std;
#define ll long long
const int maxn = 505;
char map[maxn][maxn];
bool vis[maxn][maxn];
int n, m, q, x1[maxn*maxn], y1[maxn*maxn], top, ans, an[maxn][maxn];
int dis[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
void dfs(int x, int y) {
vis[x][y] = 1;
if (map[x][y] == 'C') ans ++;
x1[top] = x, y1[top ++] = y;
for (int i = 0; i < 4; i ++) {
int xx = x + dis[i][0];
int yy = y + dis[i][1];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && map[xx][yy] != '#') {
dfs(xx, yy);
}
}
return ;
}
int main()
{
int t, Case = 0;
scanf("%d", &t);
while (t --) {
scanf("%d%d%d", &n, &m, &q);
memset(vis, 0, sizeof(vis));
memset(an, -1, sizeof(an));
for (int i = 0; i < n; i ++) {
scanf("%s", map[i]);
}
int x, y;
printf("Case %d:\n", ++ Case);
for (int i = 0; i < q; i ++) {
scanf("%d%d", &x, &y);
if (an[x - 1][y - 1] == -1) {
top = 0, ans = 0, dfs(x - 1, y - 1);
}
for (int j = 0; j < top; j ++) {
an[x1[j]][y1[j]] = ans;
}
printf("%d\n", an[x - 1][y - 1]);
}
}
}