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

最新文章

  1. IO(三)----序列流
  2. CSS样式优化
  3. python socket和socketserver
  4. Leetcode 121 Best Time to Buy and Sell Stock 动态规划
  5. Redmine backlogs 升级
  6. hibernate主键自动生成
  7. 比Spring简单的IoC容器
  8. 【python问题系列--1】SyntaxError:Non-ASCII character &#39;\xe5&#39; in file kNN.py on line 2, but no encoding declared;
  9. PHP foreach 遍历数组是打印出相同的数据
  10. php中for循环的应用
  11. SSM-Spring-19:Spring中JdbcTemplate
  12. Leetcode#461. Hamming Distance(汉明距离)
  13. golang反射
  14. VCC、VDD和VSS
  15. CSS 隐藏ul
  16. Linux安装RabbitMq-Centos7版本
  17. Nancy Web框架 文档
  18. Odoo 开源协议讨论
  19. 2014-07-08 hibernate tenancy
  20. HDU2017新生赛 找方块

热门文章

  1. 一天一个Java基础——序列化
  2. 确实是非常实用的Ubuntu命令
  3. MySQL索引与优化策略
  4. Oracle数据库启动时:ORA-00119: invalid specification for system parameter LOCAL_LISTENER; ORA-00132错误解决
  5. Spring Bean基本管理--bean注入方式汇总
  6. Dropping water balloons
  7. 修改Eclipse的EasyExplore插件的键盘快捷键
  8. 三道JS试题(遍历、创建对象、URL解析)
  9. lighttpd mod_status模块
  10. iOS 8 Xcode6 设置Launch Image 启动图片&lt;转&gt;