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.
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.
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3
6 4 10
4 4 1 **** *..* *.** **** 3 2
8
题目大意:
给你一个N*M的图,以及Q个查询,每个查询询问一个联通块周围有多少个地方能够放置图片。
能够放置图片的位子:一块空白和一块墙之间可以放置一个图片。
思路:
预处理Bfs,标记每个联通块的颜色然后遍历其周围有多少个墙即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int x,y;
}now,nex;
int vis[1500][1500];
int ans[1000*1005];
char a[1500][1500];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int sig;
int n,m,q;
void Bfs(int sx,int sy)
{
now.x=sx;
now.y=sy;
vis[now.x][now.y]=sig;
queue<node >s;
s.push(now);
while(!s.empty())
{
now=s.front();
s.pop();
for(int i=0;i<4;i++)
{
nex.x=now.x+fx[i];
nex.y=now.y+fy[i];
if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]=='.')
{
vis[nex.x][nex.y]=sig;
s.push(nex);
}
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
sig=0;
for(int i=0;i<n;i++)scanf("%s",a[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(vis[i][j]==0)
{
sig++;
Bfs(i,j);
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='.')
{
for(int k=0;k<4;k++)
{
int xx=i+fx[k];
int yy=j+fy[k];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='*')
{
ans[vis[i][j]]++;
}
}
}
}
}
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
x--,y--;
printf("%d\n",ans[vis[x][y]]);
}
}
}