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]]);
}
}

Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集的更多相关文章

  1. Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)

    题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能 ...

  2. Educational Codeforces Round 7 C. Not Equal on a Segment 并查集

    C. Not Equal on a Segment 题目连接: http://www.codeforces.com/contest/622/problem/C Description You are ...

  3. Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)

    连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...

  4. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  5. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  6. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  7. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  8. Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

  9. Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)

    链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...

随机推荐

  1. 转载RabbitMQ入门(5)--主题

    主题(topic) (使用Java客户端) 在先前的指南中我们改进了我们的日志系统.取代使用fanout类型的交易所,那个仅仅有能力实现哑的广播,我们使用一个direct类型的交易所,获得一个可以有选 ...

  2. HDU 5379 Mahjong tree

    题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...

  3. java解析XML四种方法

    XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML. XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便 ...

  4. 擦亮自己的眼睛去看SQLServer之简单Select(转)

    摘要:这篇文章主要和大家讨论几乎所有人都熟悉,但不少人又陌生的一条select语句. 这篇文章主要和大家讨论几乎所有人都熟悉,但不少人又陌生的一条select语句.不知道大家有没有想过到底是什么东西让 ...

  5. HDU 5278 PowMod 数论公式推导

    题意:中文题自己看吧 分析:这题分两步 第一步:利用已知公式求出k: 第二步:求出k然后使用欧拉降幂公式即可,欧拉降幂公式不需要互质(第二步就是BZOJ3884原题了) 求k的话就需要构造了(引入官方 ...

  6. C#条件语句、循环语句

    一.程序的三种结构 顺序结构 分支结构 循环结构 二.条件语句if 语句是最有用的控制结构之一. if … else …语句的语法: if (布尔表达式)执行操作的语句  或if (布尔表达式)执行操 ...

  7. Claim-based-security for ASP.NET Web APIs using DotNetOpenAuth

    Recently I worked with a customer assisting them in implementing their Web APIs using the new ASP.NE ...

  8. 用matlab绘制幂函数

    用matlab绘制幂函数 下周轮到我做论文汇报了,刚好前两天看了网格水印的文章,就决定汇报前两天看到的那篇论文了.在准备ppt的过程中,绘制了一些幂函数,感觉matlab真的是很强大啊,可以绘制各种曲 ...

  9. 坚持自学的第二天,bootstrap初入门

    前言 昨天,初步学完了jekyll目录结构与Liquid语法的应用与认识. 日志 今天刚入门,做了一个bootstrap导航栏,但是选中状态不行,找了JS中写好的API,写法与视频中讲的有点不一样,但 ...

  10. Java 性能分析工具

    如何利用 JConsole观察分析Java程序的运行,进行排错调优 http://jiajun.iteye.com/blog/810150 如何使用JVisualVM进行性能分析 http://jia ...