Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集

时间:2022-04-22 20:31:35

D. Igor In the Museum

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/598/problem/D

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 nm 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

HINT

题意

给你一个n*m的矩阵,.表示能走,*表示是墙,每一面墙上都挂了一幅画

然后问你k次,分别从xi,yi走最多能看多少画

题解:

直接BFS暴力预处理就好了,然后对于每次询问,我是用并查集去维护的

代码

#include<iostream>
#include<stdio.h>
#include<queue>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std; int n,m,q;
int fa[];
char s[][];
int vis[][];
int ans[];
int getid(int x,int y)
{
return x*m+y;
}
int fi(int x)
{
if(x!=fa[x])
fa[x]=fi(fa[x]);
return fa[x];
}
void uni(int x,int y)
{
int p = fi(x),q = fi(y);
if(p==q)return;
fa[y]=fa[x];
ans[y]+=ans[x];
} int dx[]={,-,,};
int dy[]={,,,-};
void bfs(int x,int y)
{
queue<pair<int,int> >Q;
Q.push(make_pair(x,y));
while(!Q.empty())
{
pair<int,int>now = Q.front();
Q.pop();
if(vis[now.first][now.second])continue;
vis[now.first][now.second]=;
for(int i=;i<;i++)
{
pair<int,int> next = now;
next.first +=dx[i];
next.second += dy[i];
if(next.first<||next.first>n)continue;
if(next.second<||next.second>m)continue;
if(vis[next.first][next.second])continue;
if(s[next.first][next.second]=='*')
{
ans[getid(x,y)]++;
continue;
}
uni(getid(x,y),getid(next.first,next.second));
Q.push(next);
}
}
}
int main()
{
memset(vis,,sizeof(vis));
memset(s,,sizeof(s));
memset(fa,,sizeof(fa));
memset(ans,,sizeof(ans));
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
fa[getid(i,j)]=getid(i,j);
}
} for(int i=;i<n;i++)
scanf("%s",s[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(s[i][j]=='*')continue;
if(vis[i][j])continue;
bfs(i,j);
}
}
for(int i=;i<q;i++)
{
int x,y;scanf("%d%d",&x,&y);x--,y--;
int k = fi(getid(x,y));
printf("%d\n",ans[fa[k]]);
}
}