[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. 若两 ...
随机推荐
- CC03 iOS推送机制浅析
• ios推送机制 可以通俗的把APNS理解为iOS系统为每个app提供的长连接通道 苹果限制了每个app在后台存活的时间,最重要的目的是为了省电,其次优化内存这些.如果彻彻底底的将app杀死了,服务 ...
- Design Patterns in Android
对日常在 Android 中实用设计模式进行一下梳理和总结,文中参考了一些网站和大佬的博客,如 MichaelX(xiong_it) .菜鸟教程.四月葡萄.IAM四十二等,在这里注明下~另外强烈推荐图 ...
- HTML标签学习总结(2)
点我:HTLM标签学习总结(1) 11. 在网页制作过程过中,可以把一些独立的逻辑部分划分出来,放在一个<div>标签中,这个<div>标签的作用就相当于一个容器. 语法: & ...
- 进阶之路 | 奇妙的Handler之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 需要已经具备的知识: Handler的基本概念及使用 学习导图: 一.为什么要学习Handler? 在Andr ...
- Springboot 2.2.x 默认不支持put、delete等请求方式
springboot 2.2.x 默认不支持put delete等请 原因:springboot默认关闭了对它们的支持,只要在application.properties里面打开即可 spring.m ...
- python学习记录_中断正在执行的代码,执行剪切板中的代码,键盘快捷键,魔术命令,输入和输出变量,记录输入和输出变量_
2018-03-28 00:56:39 中断正在执行的代码 无论是%run执行的脚本还是长时间运行的命令ctrl + cIn [1]: KeyboardInterrupt 执行剪切板中的代码 ctrl ...
- ajax的post提交 序列化json参数
再一次项目中,很常见的就是我的前端需要异步进行和后端交互 ,然而需要携带一些参数过去,并且参数类型是json 怎么办呢? 这个时候我们就需要 进行参数序列化 很简单就两句话 如下图 我们看 JSON, ...
- swoole websocket_server 聊天室--群聊
centos7 php7.2 swoole4.3 nginx1.8 websocket_server 代码 <?php $server = new Swoole\WebSocket\Serve ...
- 2020ubuntu1804server编译安装redis笔记(一)及报make test错误解决办法
redis的大名我想大家都不陌生,今天在ubuntu server上进行编译安装,虽然apt也可以安装,但作为内存数据库,redis又是c开发的,编译安装,对机器的适应和性能更好. 安装笔记如下 第1 ...
- ApplicationContextInitializer的理解和使用
一.ApplicationContextInitializer 介绍 1.1 作用 ApplicationContextInitializer 接口用于在 Spring 容器刷新之前执行的一个回调函数 ...