[BFS]Codeforces Igor In the Museum
1 second
256 megabytes
standard input
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.
First line of the input contains three integers n, m 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.
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
6
4
10
4 4 1
****
*..*
*.**
****
3 2
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的更多相关文章
- 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 ...
- 【CodeForces - 598D】Igor In the Museum(bfs)
Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能 ...
- Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
http://codeforces.com/problemset/problem/598/D 分析:BFS,同一连通区域的周长一样,但查询过多会导致TLE,所以要将连通区域的答案储存,下次查询到该连通 ...
- codeforces 598D Igor In the Museum
题目链接:http://codeforces.com/problemset/problem/598/D 题目分类:dfs 题目分析:处理的时候一次处理一片而不是一个,不然会超时 代码: #includ ...
- 【Codeforces 598D】Igor In the Museum
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...
- DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...
随机推荐
- linux Init分析(原创)
1.uboot的目标就是启动内核kernel: 2.kernel的目的就是启动应用程序,而第一个应用程序即是Init,构建根文件系统. 从uboot初始化配置后,引导内核的启动,启动函数为:start ...
- Random Forest And Extra Trees
随机森林 我们对使用决策树随机取样的集成学习有个形象的名字–随机森林. scikit-learn 中封装的随机森林,在决策树的节点划分上,在随机的特征子集上寻找最优划分特征. import numpy ...
- Hadoop环境搭建问题总结
最近抽空搭建了Hadoop完全分布式环境,期间遇到了很多问题,大部分问题还是可以在网上搜到的,这里说下自己遇到的两个没有找到结果的问题吧. 1.启动时报:没有那个文件或目录 原因:三台机器的用户名不一 ...
- js操作复选框
js操作复选框 JavaScript 代码: //复选框全选 $(function () { $("#select_all").click(function () { $(&quo ...
- 大马提权详细过程webshell到提权
.在shell路径这一栏里输入服务器端cmd.exe对应的绝对路径,这里用我们刚刚上传上去的smallchao.exe 8.WINDOWS常见命令:net user 查看所有用户query user ...
- TCP/IP协议族的四个层次
OSI7层模型的小结 : 在7层模型中,每一层都提供一个特殊的网络功能.从网络功能的角度看:下面4层(物理层.数据链路层.网络层和传输层)主要提供数据传输和交换功能,即以节点到节点之间的通信为主:第4 ...
- 整合Kafka+Flink 实例(第二部分 设计思路)
前 言 拖了蛮久了,一直说要接着上一部分写设计思路以及代码,因为自己技术底子薄弱,加上人又懒,所以一直没能继续,今天补上设计思路及部分代码,后面有时间我会再补充一些应用性的功能,的确有些忙,希 ...
- Tomcat8优化--Apache JMeter测试
一.部署测试java web项目(压力测试环境搭建) 1.mysql环境 #切换到mysql目录 cd /usr/local/mysql #查看mysql环境 rpm -qa | grep -i my ...
- xml模块介绍
# xml 是一门可拓展的语言 # xml 语法 是用<>包裹的起来的<>就是标签, xml可以由多个<>组成 也可以由单个<>组成, # < ...
- Cisco asa组建IPSEC for ikev1
IPSec的实现主要由两个阶段来完成:--第一阶段,双方协商安全连接,建立一个已通过身份鉴别和安全保护的通道.--第二阶段,安全协议用于保护数据的和信息的交换. IPSec有两个安全协议:AH和ESP ...