Description
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 Input
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
Sample Output
6
4
10
Sample Input
4 4 1
****
*..*
*.**
****
3 2
Sample Output
8
题解:
查找所给坐标与附近“.”组成的图形的周长,刚开始以为是个规律题,后来发现许多特殊情况,应该用搜索做。按点查询其周围的边。
代码:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 100007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;
char s[1111][1111];
int fx[4] = {0,0,-1,1};
int fy[4] = {-1,1,0,0};
int vis[1111][1111],n,m,k,ans[111111],cnt;
void dfs(int x,int y)
{
vis[x][y]=cnt;
for(int i=0; i<4; i++)
{
int xx=x+fx[i];
int yy=y+fy[i];
if(s[xx][yy]=='*')
ans[cnt]++;
if(s[xx][yy]=='.'&&!vis[xx][yy])
dfs(xx,yy);
}
}
int main()
{
cin>>n>>m>>k;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin>>s[i][j];
while(k--)
{
int x,y;
cin>>x>>y;
if(vis[x][y])
cout<<ans[vis[x][y]]<<endl;
else
{
cnt++;
dfs(x,y);
cout<<ans[cnt]<<endl;
}
}
return 0;
}