Igor In the Museum
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input

Copy
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
output

Copy
6
4
10
input

Copy
4 4 1
****
*..*
*.**
****
3 2
output

Copy
8

题意:

在一个有n*m的格子(这些格子是矩形且每个格子与周围的4个格子邻接)平面内,能走的格子为'.',不能走的格子为'#'称为墙,有多个起点,问若从一个起点出发,能找到多少与墙相邻的边

思路:

先读入整个地图
注意要先把整个地图读完在看墙的上下左右,因为如果边度边判断就无法判断下面和右边的情况(你还没读到,现在是不存在的)
如果当前这个是墙,则其上下左右在n*m的格子内的可走区域的答案要+1
接着读入起始点
若这个格子以前被走过了就直接输出答案
否则就从这个格子开始搜索并把一开始处理好的墙边上可走的格子的答案加上,并把当前能走到的点都更新为这个答案,因为从这些点出发都能得到这个最大答案

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e3+;
char mp[amn][amn];
long long ans[amn][amn],n,m,k,as,sx,sy;
bool idx[amn][amn],f[amn][amn];
int dic[][]={{,-},{,+},{+,},{-,}};
struct node{
int x;int y;
};
queue<node> q;
long long fd(int x,int y){
node a;
a.x=x;a.y=y;
while(q.size())q.pop();
q.push(a);
long long cnt=;
while(q.size()){
node w=q.front();q.pop();
//cout<<w.x<<'!'<<w.y<<endl;
f[w.x][w.y]=idx[w.x][w.y]=;
if(ans[w.x][w.y])cnt+=ans[w.x][w.y];
for(int k=;k<;k++){
int dx=w.x+dic[k][];
int dy=w.y+dic[k][];
//cout<<k<<' '<<w.x<<' '<<w.y<<' '<<dx<<'@'<<dy<<' '<<cnt<<endl;
if(dx>&&dx<n&&dy>&&dy<m&&mp[dx][dy]=='.'&&!idx[dx][dy]&&!f[dx][dy]){
f[dx][dy]=idx[dx][dy]=;
a.x=dx;a.y=dy;
q.push(a);
}
}
}
return cnt;
}
void cg(int x,int y,long long val){
node a;
a.x=x;a.y=y;
while(q.size())q.pop();
q.push(a);
while(q.size()){
node w=q.front();q.pop();
//cout<<w.x<<'?'<<w.y<<endl;
idx[w.x][w.y]=;
ans[w.x][w.y]=val;
for(int k=;k<;k++){
int dx=w.x+dic[k][];
int dy=w.y+dic[k][];
if(dx>=&&dx<=n&&dy>=&&dy<=m&&mp[dx][dy]=='.'&&idx[dx][dy]&&f[dx][dy]){
idx[dx][dy]=; ///idx是搜索标记,注意只有idx还原为0,f数组是看这个答案是否存在,只要有答案就不需要更改了
a.x=dx;a.y=dy;
q.push(a);
}
}
}
}
int main(){
ios::sync_with_stdio();
cin>>n>>m>>k;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
f[i][j]=idx[i][j]=ans[i][j]=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>mp[i][j]; ///先读入整个地图
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){ ///注意要先把整个地图读完在看墙的上下左右,因为如果边度边判断就无法判断下面和右边的情况(你还没读到,现在是不存在的)
if(mp[i][j]=='*'){ ///如果当前这个是墙,则其上下左右在n*m的格子内的可走区域的答案要+1
for(int k=;k<;k++){
int dx=i+dic[k][];
int dy=j+dic[k][];
if(dx>=&&dx<=n&&dy>=&&dy<=m&&mp[dx][dy]=='.')ans[dx][dy]++;//cout<<dx<<' '<<dy<<' '<<ans[dx][dy]<<endl;;
}
}
}
}
while(k--){
cin>>sx>>sy;
if(f[sx][sy])printf("%lld\n",ans[sx][sy]); ///若这个格子以前被走过了就直接输出答案
else{ ///否则就从这个格子开始搜索并把一开始处理好的墙边上可走的格子的答案加上,并把当前能走到的点都更新为这个答案,因为从这些点出发都能得到这个最大答案
as=fd(sx,sy);
cg(sx,sy,as);
printf("%lld\n",ans[sx][sy]);
}
}
}
/***
在一个有n*m的格子(这些格子是矩形且每个格子与周围的4个格子邻接)平面内,能走的格子为'.',不能走的格子为'#'称为墙,有多个起点,问若从一个起点出发,能找到多少与墙相邻的边
先读入整个地图
注意要先把整个地图读完在看墙的上下左右,因为如果边度边判断就无法判断下面和右边的情况(你还没读到,现在是不存在的)
如果当前这个是墙,则其上下左右在n*m的格子内的可走区域的答案要+1
接着读入起始点
若这个格子以前被走过了就直接输出答案
否则就从这个格子开始搜索并把一开始处理好的墙边上可走的格子的答案加上,并把当前能走到的点都更新为这个答案,因为从这些点出发都能得到这个最大答案
***/

[BFS]Codeforces Igor In the Museum的更多相关文章

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

    D. Igor In the Museum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598 ...

  2. 【CodeForces - 598D】Igor In the Museum(bfs)

    Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...

  3. Codeforces 598D:Igor In the Museum

    D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)

    D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...

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

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

  6. Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum

    http://codeforces.com/problemset/problem/598/D 分析:BFS,同一连通区域的周长一样,但查询过多会导致TLE,所以要将连通区域的答案储存,下次查询到该连通 ...

  7. codeforces 598D Igor In the Museum

    题目链接:http://codeforces.com/problemset/problem/598/D 题目分类:dfs 题目分析:处理的时候一次处理一片而不是一个,不然会超时 代码: #includ ...

  8. 【Codeforces 598D】Igor In the Museum

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...

  9. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

随机推荐

  1. JavaScript 設計模型 - Iterator

    Iterator Pattern是一個很重要也很簡單的Pattern:迭代器!我們可以提供一個統一入口的迭代器,Client只需要知道有哪些方法,或是有哪些Concrete Iterator,並不需要 ...

  2. XX系统测试总结报告

    XX系统测试总结报告 1        引言 1.1  编写目的 编写该测试总结报告主要有以下几个目的 1.  通过对测试结果的分析,得到对软件质量的评价 2.   分析测试的过程,产品,资源,信息, ...

  3. scss入门学习(一)

    sass的文件后缀名 sass是目前流行的css预处理语言.sass有两种后缀文件,一种是.sass,不允许使用大括号和分号:另一种是.SCSS,允许使用大括号和分号,类似于我们平时写css的语法习惯 ...

  4. android使用giflib加载gif

    转载请标明出处:https:////www.cnblogs.com/tangZH/p/12356915.html 背景不多说,反正ndk加载gif比java上加载gif好很多很多,主要体现在内存占用与 ...

  5. 等效燃油消耗ECMS与庞德里亚金最小值原理PMP中协同状态的关系

    今儿阅读了一篇文献——<车联网环境下并联混合动力客车控制策略优化研究>,是北理的博士所写的,内容比较翔实.主要是里面的关于ECMS和庞德里亚金最小值原理(PMP)的关系推导很让人印象深刻, ...

  6. AI:是猫还是狗,这是个问题

    如果你不喜欢小猫和小狗,你可能不知道他们具体是哪一种品种,但是一般来说,你都能区分出这是猫还是狗,猫和狗的特征还是不一样的,那我们如何用机器学习的方法训练一个网络区分猫狗呢? 我们选用的是 Kaggl ...

  7. python 生成随机数、生成 uuid

    1. 使用 uuid.uuid1 产生一个随机数 2. 在使用 random.sample() 产生一个随机字符串 3. 将两者进行拼接 import uuid import random def r ...

  8. 从0开发3D引擎(补充):介绍领域驱动设计

    我们使用领域驱动设计(英文缩写为DDD)的方法来设计引擎,在引擎开发的过程中,领域模型会不断地演化. 本文介绍本系列使用的领域驱动设计思想的相关概念和知识点,给出了相关的资料. 上一篇博文 从0开发3 ...

  9. 前阿里数据库专家总结的MySQL里的各种锁(上篇)

    0.前言 MySQL按照加锁的范围,分为全局锁.表级锁.行级锁. 本文作为上篇,主要介绍MySQL的全局锁 和 表级锁. 重要的实战总结为,如何安全地变更一个表的表结构. 1.全局锁 定义: 全局锁就 ...

  10. HTML与CSS 开发常用语义化命名

    一.布局❤️ header 头部/页眉:index 首页/索引:logo 标志:nav/sub_nav 导航/子导航:banner 横幅广告:main/content 主体/内容:container/ ...