【CodeForces - 598D】Igor In the Museum(bfs)
Igor In the Museum
Descriptions
给你一个n*m的方格图表示一个博物馆的分布图.
每个方格上用'*'表示墙,用'.'表示空位.
每一个空格和相邻的墙之间都有一幅画.
(相邻指的是上下左右相邻).
你可以从一个空格的位置走到相邻的空格位置.
现在你给你若干个(xi,yi)形式的询问,表示你现在在(xi,yi)这个位置(保证为空位)出发,问你从这个点出发你能看到多少幅画.
Input
第一行有3个整数n,m,k(3<=n,m<=1000,1<=k<=min(m*m,100000) ).
接下来有n行每行m个字符,每个字符为'.'或者'*'.
紧接着k行,每行两个整数xi,yi.
Output
对于k个询问,输出相应的答案.
Examples
Input5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
Output6
4
10Input4 4 1
****
*..*
*.**
****
3 2
Output8
题目链接
https://vjudge.net/problem/CodeForces-598D
不难的一个bfs,一直t在memset上,每次bfs是不需要memset标记数组的,只要你记录一下,每个点就只需要扫一次了,直接一整个幅地图按块bfs,即从这一块的"."出发,看到的都是ans副画,并且记录下来,最后直接输出即可。还不清楚可以参考代码
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1005
using namespace std;
int n,m,k;
char mp[Maxn][Maxn];//存图
int vis[Maxn][Maxn];//标记"."是否走过
int step;//几副画
int dt[][]= {{,},{,-},{,},{-,}};//四个方向
struct node
{
int x,y;
};
node now,net;
node road[];//"."这个点的状态
int ans[Maxn][Maxn];//记录从(x,y)能看到几幅画
void judge(int x,int y)//(x,y)这四周有几幅画
{
for(int i=; i<; i++)
{
int tx=dt[i][]+x;
int ty=dt[i][]+y;
if(tx>=&&ty>=&&tx<=n&&ty<=m&&mp[tx][ty]=='*')
{
step++;
}
}
}
void bfs()
{
step=;//几幅画
int cnt=;//第几个"."
queue<node>q;
q.push(now);
judge(now.x,now.y);
vis[now.x][now.y]=;
while(!q.empty())
{
now=q.front();
q.pop();
road[cnt++]=now;
for(int i=; i<; i++)//四个方向bfs
{
int tx=dt[i][]+now.x;
int ty=dt[i][]+now.y;
if(tx>=&&ty>=&&tx<=n&&ty<=m&&!vis[tx][ty]&&mp[tx][ty]=='.')
{
net.x=tx,net.y=ty;
q.push(net);
judge(tx,ty);
vis[tx][ty]=;
} }
}
for(int i=; i<cnt; i++)//这一块的"."全部都能看见step副画
ans[road[i].x][road[i].y]=step;
}
int main()
{
MEM(vis,);//初始化,存图
cin>>n>>m>>k;
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
cin>>mp[i][j];
for(int i=; i<=n; i++)//开始一块一块的找"."并且bfs
{
for(int j=; j<=m; j++)
{
if(mp[i][j]=='.'&&!vis[i][j])
{
now.x=i,now.y=j;
bfs();
}
}
}
while(k--)
{
int x,y;
cin>>x>>y;
cout<<ans[x][y]<<endl;
}
return ;
}
【CodeForces - 598D】Igor In the Museum(bfs)的更多相关文章
- 【Codeforces 598D】Igor In the Museum
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...
- 【CodeForces - 501B 】Misha and Changing Handles(map)
Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description 神秘的三角洲里还有一个传说 ...
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- 【洛谷】P1379 八数码难题(bfs)
题目 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局 ...
- 【Unity3D实战】摇摆直升机开发实战(一)
[Unity3D实战]摇摆直升机开发实战(一) 1.点击[Assets],创建[Sprites]和[Resources]文件夹,然后将所需要的素材导入[Sprites]文件夹中. 2.找到[Sprit ...
- 【gdoi2018 day2】第二题 滑稽子图(subgraph)(性质DP+多项式)
题目大意 [gdoi2018 day2]第二题 滑稽子图(subgraph) 给你一颗树\(T\),以及一个常数\(K\),对于\(T\)的点集\(V\)的子集\(S\). 定义\(f(S)\)为点集 ...
- 【详解】ThreadPoolExecutor源码阅读(三)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 线程数量的 ...
- 【详解】ThreadPoolExecutor源码阅读(二)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) AQS在W ...
- 【详解】ThreadPoolExecutor源码阅读(一)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 工作原理简 ...
随机推荐
- Linux初始化脚本
以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行.脚本结合各位大牛一些参数,已经在CentOS 5下通过. 使用方法:将其复制,保存为一个shell文件,比如init.sh ...
- 解读>/dev/null 2>&1
背景 我们经常能在shell脚本中发现>/dev/null 2>&1这样的语句.以前的我并没有去深入地理解这段命令的作用,照搬照用,今天开始去解读>/dev/null 2&g ...
- [BZOJ 1146] [CTSC2008]网络管理Network(树状数组+主席树)
题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...
- String替换占位符
/** * 依次替换占位符 * 例如: 姓名:{s},电话:{s},邮箱:{s} --> 姓名:小张,电话:18800000001,邮箱:abc@123.com * pattern = &quo ...
- httpclient发邮件
package com.chuanglan; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.L ...
- binlog2sql闪回工具的使用
binlog2sql闪回工具的使用 一.下载安装依赖的python yum install openssl-devel bzip2-devel expat-devel gdbm-devel readl ...
- 01_Deepin15 下搭建python开发环境
https://blog.csdn.net/iimpact/article/details/90239193 https://github.com/josonle/Coding-Now#Linux系统 ...
- IDEA配置和插件
1.相关配置 设置字体和大小 2.插件 maven helper 解决maven包冲突的问题 打开pom文件,并可以切换tab,简单使用,如下图 RestfulToolkit RestfulToolk ...
- 爬虫代理池源代码测试-Python3WebSpider
元类属性的使用 来源: https://github.com/Python3WebSpider/ProxyPool/blob/master/proxypool/crawler.py 主要关于元类的使用 ...
- LA 7263 Today Is a Rainy Day bfs+暴力 银牌题
7263 Today Is a Rainy Day Today is a rainy day. The temperature is apparently lower than yesterday. ...