题目链接:http://lightoj.com/volume_showproblem.php?problem=1337

思路:对于搜过的区域进行标记,如果要求的点落在已经搜过的区域,那么直接取出来即可,否则,就dfs一下。

 #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ( + );
int n, m, Q, _count, cnt;
char map[MAXN][MAXN];
int mark[MAXN][MAXN];
vector<int >ans;
int dir[][] = { { -, }, { , }, { , - }, { , } }; void dfs(int x, int y)
{
mark[x][y] = _count;
if (map[x][y] == 'C') cnt++;
for (int i = ; i < ; i++) {
int xx = x + dir[i][];
int yy = y + dir[i][];
if (xx >= && xx < n && yy >= && yy < m && map[xx][yy] != '#') {
if (mark[xx][yy] == -)dfs(xx, yy);
}
}
} int main()
{
int _case, t = ;
scanf("%d", &_case);
while (_case--) {
scanf("%d %d %d", &n, &m, &Q);
for (int i = ; i < n; i++) {
scanf("%s", map[i]);
}
memset(mark, -, sizeof(mark));
ans.clear();
_count = -;
printf("Case %d:\n", t++);
while (Q--) {
int x, y;
scanf("%d %d", &x, &y);
x--, y--;
if (map[x][y] == '#') {
puts("");
}
else if (mark[x][y] == -) {
_count++;
cnt = ;
dfs(x, y);
ans.push_back(cnt);
printf("%d\n", cnt);
}
else {
printf("%d\n", ans[mark[x][y]]);
}
}
}
return ;
}
04-17 03:18