【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数

来源:互联网 发布:结构化编程 编辑:程序博客网 时间:2024/05/17 04:03
D. Igor In the Museum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor is in the museum and he wants to see as many pictures as possible.

Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.

At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.

For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.

Input

First line of the input contains three integers n, m and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.

Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.

Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.

Output

Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.

Sample test(s)
input
5 6 3*******..*.********....*******2 22 54 3
output
6410
input
4 4 1*****..**.******3 2
output
8

#include<stdio.h> #include<string.h>#include<ctype.h>#include<math.h>#include<iostream>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=1010,M=0,Z=1e9+7,ms63=1061109567;const int dy[4]={-1,0,0,1};const int dx[4]={0,-1,1,0};int casenum,casei;char a[N][N];int vis[N][N];int rec[N*N];int ans[N][N];int n,m,q,tim,num;void dfs(int y,int x){vis[y][x]=tim;for(int i=0;i<4;i++){int yy=y+dy[i];int xx=x+dx[i];if(a[yy][xx]=='*')++num;else if(!vis[yy][xx])dfs(yy,xx);}}int main(){while(~scanf("%d%d%d",&n,&m,&q)){MS(vis,0);tim=0;for(int i=1;i<=n;i++)scanf("%s",a[i]+1);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)if(a[i][j]=='.'){if(vis[i][j]==0){++tim;num=0;dfs(i,j);rec[tim]=num;}ans[i][j]=rec[vis[i][j]];}}int y,x;while(q--){scanf("%d%d",&y,&x);printf("%d\n",ans[y][x]);}}return 0;}/*【题意】给你一个n(1000)*m(1000)的地图,有k(1e5)个询问,地图上的'*'代表墙,'.'代表空地。一个墙最多有4个面。*与.相邻的就是其一个面。对于每个询问,给出一个范围内的坐标(y,x),问你,从这个开始向四周走,能撞到多少面墙。【类型】简单dfs【分析】直接从每个'.'开始dfs,dfs到的'.'都打上同样的标记,表示其为是相同联通块,相同连通块的答案是相同的。同时对墙的每一面用bool数组打标记,被第一次搜到的话就用全局变量计数++这个全局变量用来更新这个联通块的答案。这样这道题就做完啦。然后我们发现,因为每个'.'最多走一次,所以其实每面墙也只最多计数1次,它最多只会对一个联通块的答案贡献1.【时间复杂度&&优化】O(4nm)*/


1 0