Igor In the Museum
题意:在一个n*m的画展馆里,“ . ”表示空地,“ * ”表示墙,每一个空地旁边的墙面上都有一幅画。给出一个空地点的坐标,问这个点所在的空地区域旁边的墙上一共有多少副画。
1 #include<algorithm> 2 #include<cstdio> 3 #include<iostream> 4 #include<queue> 5 #include<vector> 6 #include<map> 7 #include<stack> 8 #include<string> 9 #include<cstring> 10 #define ll long long 11 using namespace std; 12 const int maxn = 1e5 + 10; 13 int n, m, k,vis[1010][1010]; 14 char mp[1010][1010]; 15 int ans = 0; 16 int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} }; 17 struct node { 18 int a, b; 19 }temp,nx; 20 int cnt = 1;int key[maxn]; 21 void bfs(int x, int y) { 22 queue<node>q; 23 q.push({ x,y }); 24 while (!q.empty()) { 25 temp = q.front(); 26 q.pop(); 27 if (vis[temp.a][temp.b] == 0) { 28 vis[temp.a][temp.b] = cnt; 29 for (int i = 0;i < 4;i++) { 30 nx.a = temp.a + dir[i][0]; 31 nx.b = temp.b + dir[i][1]; 32 if (mp[nx.a][nx.b] == '*')ans++; 33 if (vis[nx.a][nx.b] == 0 && mp[nx.a][nx.b] != '*')q.push(nx); 34 } 35 } 36 } 37 key[cnt++] = ans; 38 } 39 int main() 40 { 41 scanf("%d%d%d", &n, &m, &k); 42 for (int i = 1;i <= n;i++)scanf("%s", mp[i] + 1); 43 while (k--) { 44 int x, y; 45 scanf("%d%d", &x, &y); 46 if (vis[x][y] != 0) { 47 printf("%d\n",key[vis[x][y]]); 48 } 49 else { 50 ans = 0; 51 bfs(x, y); 52 printf("%d\n", ans); 53 } 54 } 55 }