Easy to find the answer is equal to the same communication block, it is possible to answer a traverse FIG empty point and save, save the method may create an array, the following table is the block number corresponding to the communication
#include<iostream> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<algorithm> #include<queue> #include<stack> #include<sstream> #include<cstdio> #define INF 0x3f3f3f3f #define read(i) scanf("%d",&i) const int maxn = 1e6 + 5; const double PI = acos(-1.0); typedef long long ll; using namespace std; int everything; int cnt = 1 ; int n, m; int vis [ 1005 ] [ 1005 ]; char mp [ 1005 ] [ 1005 ]; int nothing [ 1000000 + 5 ]; int dx [] = {- 1 , 0 , 1 , 0 }; int dy [] = { 0 , 1 , 0 , - 1 }; int in(int x, int y) { return x > 0 && x <= n && y > 0 && y <= m; } void dfs(int x, int y) { vis[x][y] = cnt; for (int i = 0; i < 4; i++) { int xx = x + dx[i], yy = y + dy[i]; if (in(xx, yy) && !vis[xx][yy]) { //printf("%d %d", xx, yy); if (mp[xx][yy] == '.') dfs(xx, yy); else if (mp[xx][yy] == '*') tot++; } } } int main() { int k; read(n), read(m), read(k); for (int i = 1; i <= n; i++) { scanf("%s", mp[i] + 1); } int x, y; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (mp[i][j] == '.' && !vis[i][j]) { to = 0 ; dfs(i, j); nothing [ ++ cnt] = all; } } } while (k--) { read(x), read(y); printf("%d\n",res[vis[x][y]+1]); } return 0; }