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

Input
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
Output
6
4
10
Input
4 4 1
****
*..*
*.**
****
3 2
Output
8

题目链接

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)的更多相关文章

  1. 【Codeforces 598D】Igor In the Museum

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...

  2. 【CodeForces - 501B 】Misha and Changing Handles(map)

    Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description  神秘的三角洲里还有一个传说 ...

  3. 【CodeForces - 682C】Alyona and the Tree(dfs)

    Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...

  4. 【洛谷】P1379 八数码难题(bfs)

    题目 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局 ...

  5. 【Unity3D实战】摇摆直升机开发实战(一)

    [Unity3D实战]摇摆直升机开发实战(一) 1.点击[Assets],创建[Sprites]和[Resources]文件夹,然后将所需要的素材导入[Sprites]文件夹中. 2.找到[Sprit ...

  6. 【gdoi2018 day2】第二题 滑稽子图(subgraph)(性质DP+多项式)

    题目大意 [gdoi2018 day2]第二题 滑稽子图(subgraph) 给你一颗树\(T\),以及一个常数\(K\),对于\(T\)的点集\(V\)的子集\(S\). 定义\(f(S)\)为点集 ...

  7. 【详解】ThreadPoolExecutor源码阅读(三)

    系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 线程数量的 ...

  8. 【详解】ThreadPoolExecutor源码阅读(二)

    系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) AQS在W ...

  9. 【详解】ThreadPoolExecutor源码阅读(一)

    系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 工作原理简 ...

随机推荐

  1. SpringBoot + Maven + Hibernate ( 简单实现CRUD功能 )

    工具:idea.mariadb数据库 创建一个项目 ( student ) ........(使用idea创建一个springboot项目,这里我就不多说了) Maven 中的依赖 <?xml ...

  2. 用vs2013开启一个C拖控件的项目

    visual studio作为一款集成开发环境备受青睐,笔者尤其喜爱它的拖控件功能,程序员应该追求业务逻辑和实际功能的优化,而不是把时间消耗在编写窗体和按钮上 笔者曾翻阅中关村图书大厦,西单图书大厦, ...

  3. 莫比乌斯函数介绍&&基础

    定义 设正整数$N$按照算术基本定理分解质因数为$N=p_1^{c_1}p_2^{c_2} \cdots P_m^{c_m}$,定义函数: $$\mu(N)= \left\{\begin{matrix ...

  4. 正整数n拆分成几个不同的平方数——DFS&&打表

    考虑将正整数n拆分成几个不同的平方数之和,比如30=1^2 + 2^2 + 5^2=1^2 + 2^2 + 3^2 + 4^2,而8不存在这样的拆分. #include<bits/stdc++. ...

  5. Python 3 格式化字符串的几种方法!

    Python 3 格式化字符串的几种方法! %s和%d,%s是用来给字符串占位置,%d是给数字占位置,简单解释下: a = 'this is %s %s' % ('an','apple') 程序输出的 ...

  6. nginx大概工作机制

    1.master和worker nginx启动后,会有2种进程:worker和master;worker可能有多个:

  7. 在 CentOS 7 上安装 RabbitMQ

    RabbitMQ 服务器在安装之前需要安装 erlang. 最新版本的 RabbitMQ 3.8.0 需要 Erlang 21.3 以上的版本支持. 在这里,我们需要在你的 CentOS 中安装 Er ...

  8. Casual Literary Notes

    写在前面的话: 人生中总会有一些惊喜,它会给予坚守的人们以奖励,提醒着人们,生命中不光是辛劳和付出. 很多收获,最后看来,往往都是因为当初的那一点坚持. -- 雷宇<现场> 1.18 上午 ...

  9. 【CUDA 基础】6.0 流和并发

    title: [CUDA 基础]6.0 流和并发 categories: - CUDA - Freshman tags: - 流 - 事件 - 网格级并行 - 同步机制 - NVVP toc: tru ...

  10. BZOJ 2402 陶陶的难题II (树链剖分、线段树、凸包、分数规划)

    毒瘤,毒瘤,毒瘤-- \(30000\)这个数据范围,看上去就是要搞事的啊... 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2402 ...