[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. 若两 ...
随机推荐
- iOS自动化登录测试demo
<软件自动化测试开发>出版了 测试开发公开课培训大讲堂 微信公众号:测试开发社区 测试开发QQ群:173172133 咨询QQ:7980068 咨询微信:zouhui1003it
- Hexo+Git一个小时快速搭建个人博客
搭建本地环境:Hexo框架 Hexo为何物 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用Markdown解析文章,并瞬间利用靓丽的主题生成静态网页.其中,Markdown是一个用于将普通 ...
- 一个很实用的css技巧简析
我是小雨小雨,专注于更新有趣.实用内容的小伙,如果内容对大家有一点帮助,那么就请动动手指,给个关注.点赞支持一下吧. ^ - ^ 序言 前两天接到一个需求,其中包括一个有序的列表,我们今天就来看看这个 ...
- 8.【Spring Cloud Alibaba】配置管理-Nacos
使用Nacos管理配置 架构图 配置文件遵循的格式 bootstrap.yml pom.xml <dependency> <groupId>org.springframewor ...
- Python 爬虫 selenium 笔记
1. selenium 安装, 与文档 pip install selenium Selenium with Python中文翻译文档 selenium官网英文文档 2. selenium 的第一个示 ...
- koa01
1.koa简介 koa是express团队开发的一个更加轻量级的服务端开发框架,也是未来的趋势 2.安装 npm i -g koa-generator //全局安装koa脚手架 3.创建项目 koa2 ...
- 利用ajax 引入静态页公共的头部与底部
利用ajax引入公共的头部与底部或者多个页面需要用到的重复的组件,对于新入门的前端来说是很实用的方法,自己也是新手菜鸟一枚,折腾了好久,实现的方法有很多种,这是我个人觉得比较简单方便的 首先得把公用的 ...
- Serializable详解(1):代码验证Java序列化与反序列化
说明:本文为Serializable详解(1),最后两段内容在翻译上出现歧义(暂时未翻译),将在后续的Serializable(2)文中补充. 介绍:本文根据JDK英文文档翻译而成,本译文并非完全按照 ...
- node打开本地应用程序
1.打开浏览器 最简单的方法: const cp = require('child_process') cp.exec('start http://127.0.0.1:8889/'); // 自动打开 ...
- java中的对象 方法 引用 等一些抽象的概念是什么意思呢?
2020-03-14 最近这一段时间有点忙,好久都没有更新博客了,之后我会一直坚持下去的,和大家一同进步的. 这段时间一直在学java,相信刚开始学习java的小白,刚开始接触那么些抽象的概念一定和我 ...