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. Android Studio进行NDK编程

  2. Informatica元数据库解析

    Informatica所有的元数据信息均以数据库表的方式存到了元数据库中.当然Infa本身工具提供了很多的人性化的功能,使我们在开发时可以很方便的进行操作,但人们的需求总是万变的,需要方便的取到自己需 ...

  3. 云计算服务模型,第 1 部分: 基础架构即服务(IaaS)

    英文原文:Cloud computing service models, Part 1: Infrastructure as a Service 本文介绍三个云类别中的第一个:基础架构即服务(infr ...

  4. 修改Oracle 表空间名称 tablespace name

    修改表空间名称步骤如下: 1. 使用oracle用户登录执行 $sqlplus / as sysdba 2. 执行修改表空间命令如下 SQL> alter tablespace  TEST re ...

  5. 《Python基础教程(第二版)》学习笔记 -> 第七章 更加抽象

    对象的魔力 多态:意味着可以对不同类的对象使用同样的操作: 封装:对外部世界隐藏对象的工作细节: 继承:以普通的类为基础建立专门的类对象 多态① 多态和方法绑定到对象特性上面的函数称为方法(metho ...

  6. T-SQL 常用语句学习

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server ---  ...

  7. 如何用Entity Framework 6 连接Sqlite数据库[转]

    获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...

  8. JDBC中DAO事务函数模版

    DAO事物函数模版1: public void OrderFinsByPage(){ Connection conn = null; PreparedStatement pstmt = null; R ...

  9. TRANSLATE

    语法格式: TRANSLATE(expr, from_string, to_string) 示例如下: SELECT TRANSLATE('ab 你好 bcdefg', 'abcdefg', '123 ...

  10. Python中的__init__,__call__

    __init__函数 当一个类实例被创建时, __init__() 方法会自动执行,在类实例创建完毕后执行,类似构建函数.__init__() 可以被当成构建函数,不过不象其它语言中的构建函数,它并不 ...