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. iOS AFNetworking的使用

    转:http://www.cnblogs.com/lookenwu/p/3927897.html AFNetworking几乎是iOS上最常用的HTTP库了,AFNetworking也确实用起来简单, ...

  2. 定义页面的Dispose方法:[before]unload事件启示录

    前言 最近实施的同事报障,说用户审批流程后直接关闭浏览器,操作十余次后系统就报用户会话数超过上限,咨询4A同事后得知登陆后需要显式调用登出API才能清理4A端,否则必然会超出会话上限. 即使在页面上增 ...

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

    摘要:本来是打算先写SQLServer历史的,不过感觉写那部分内容比较难还需要多查些资料.于是调整了下顺序写下简单的Insert语句. 不过感觉写那部分内容比较难还需要多查些资料.于是调整了下顺序写下 ...

  4. cocos2d-x 详解之 CCLayer(触摸事件)

    CCLayer继承自CCNode,在CCLayer中可以实现单点触摸.多点触摸和重力感应回调3种不同形式的交互.这部分的难点在于,当存在多个层都要去接收触摸时它的响应机制是如何处理的.了解内部的处理机 ...

  5. js中的String数据类型

    string中包含一些特殊的字符字面量,又叫转义序列,\n 意思是换行,\t 意为制表,\b意为空格,\r回车,\\斜杠. 1.ECMAScript中字符串是不可变的. 2.转换字符串的方法:toSt ...

  6. webstorm下设置sass

    关于sass,就不想多说什么了.只要你有css基础,十分钟入门好吗.可以参考下资料:http://www.w3cplus.com/sassguide/ 今天想说的是webStorm下如何实现sass自 ...

  7. J2SE7规范_2013.2_类

    8.1 类的定义   包括普通类和枚举类,枚举(略) 下面都是指普通类:   public只能用于外部类,成员类,不能用于局部类,匿名类 protected和private用于成员类时(待解) sta ...

  8. Spring学习笔记(二)Spring基础AOP、IOC

    Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...

  9. 集合-Collection

    集合中存放的依然是对象的引用,而不是对象本身 ArrayList: 1) ArrayList底层使用数组实现,当使用不带参数的构造方法生成ArrayList对象时,实际上会在底层生成一个长度为10的O ...

  10. Mac vim iterm2配色方案

    转自:http://www.vpsee.com/2013/09/use-the-solarized-color-theme-on-mac-os-x-terminal/ 相信长期浸泡在终端和代码的小伙伴 ...