[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. 若两 ...
随机推荐
- Coding and Paper Letter(十五)
资源整理. 1.Nature Climate Change论文"Higher temperatures increase suicide rates in the United States ...
- linux lsof常用方法
lsof简介 lsof(list open files)是一个列出当前系统打开文件的工具,在linux环境下,任何事物都是以文件形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.系统 ...
- Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表
上一节我们介绍了如何在Spring Boot中使用模板引擎Thymeleaf开发Web应用的基础.接下来,我们介绍一下后端开发经常会遇到的一个场景:可视化图表. 通常,这类需求在客户端应用中不太会用到 ...
- PxCook+photoshop实现傻瓜式切图(推荐小白使用)
确定需求 刚入门前端的小伙伴经过一个阶段的学习,已经准备小试牛刀了.但看到设计师给出的psd图,又头疼了,天啊撸,怎么办,我不会切图啊.今天我就带领小白学习傻瓜式切图.包学包会.( ̄▽ ̄)" ...
- flask前端上传图片/文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python读入写入中文名图片
import os import cv2 import numpy as np # 读入中文命名图片 def cv_imread(in_path): cv_img = cv2.imdecode(np. ...
- docker 学习路线
docker 学习路线 参考资料 知乎 docker 的学习路线 Docker - 从入门到实践 Docker 核心技术与实现原理 Docker 入门 <Kubernetes in Action ...
- python 深浅拷贝 元组 字典 集合操作
深浅拷贝 :值拷贝 :ls = [,,] res = ls 则print(res)就是[,,] 浅拷贝 :ls.copy() 深拷贝:ls3 = deepcopy(ls) # 新开辟列表空间,ls列表 ...
- asp.net mvc core 管道以及拦截器初了解
今天来看一下asp.net core的执行管道.先看下官方说明: 从上图可以抛光,asp.net core的执行顺序是,当收到一个请求后,request请求会先经过已注册的中间件,然后会进入到mvc的 ...
- CODING 携手优普丰,道器合璧打造敏捷最佳实践
随着全球进入到信息化时代,越来越多的企业迫切地寻求新的商业模式,要求迭代.探索.不断加速创新以响应快速变化的市场.如今一系列新兴概念如敏捷开发.极限编程.微服务.自动化.DevOps 等大行其道,然而 ...