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. Filtering Approaches for Real-Time Anti-Aliasing(2011 SIGGRAPH)

    Filtering Approaches for Real-Time Anti-Aliasing(2011 SIGGRAPH) 在2011的SIGGRAPH上,NVIDA提出了FXAA3.1,本文主要 ...

  2. libev+TCP服务器事件轮询实例demo

    #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <std ...

  3. quartz (从原理到应用)详解篇(转)

    一.Quartz 基本介绍 1.1 Quartz 概述 1.2 Quartz特点 1.3 Quartz 集群配置 二.Quartz 原理及流程 2.1 quartz基本原理 2.2 quartz启动流 ...

  4. map(callback)将一组元素转换成其他数组(不论是否是元素数组)

    map(callback) 概述 将一组元素转换成其他数组(不论是否是元素数组) 你可以用这个函数来建立一个列表,不论是值.属性还是CSS样式,或者其他特别形式.这都可以用'$.map()'来方便的建 ...

  5. vue中前进刷新、后退缓存用户浏览数据和浏览位置的实践

    vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 2018年07月07日 11:58:40 大灰狼的小绵羊哥哥 阅读数:4492   vue中,我们所要实现的一个场景就是: 1.搜索页面==&g ...

  6. linux系统编程--线程

    安装线程man page,命令:sudo apt-get install manpages-posix-dev 线程概念 什么是线程 LWP:light weight process 轻量级的进程,本 ...

  7. 下载PDF格式的Html

    下载PDF格式的Html 首先准备需要的两个js jsPdf.debug.js html2canvas.js 直接上代码: function download() { html2canvas(docu ...

  8. Go中&和*的区别

    & 返回变量的内存地址 * 返回变量的值, * 只能作用在指针上 package main import "fmt" func main() { var a = 5 var ...

  9. 【java设计模式】-02工厂模式

    工厂模式简述 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客 ...

  10. springMVC课程笔记(二)springMVC组件配置

    1.springMVC的DispatcherServlet前段控制器配置,如下图所示在web.xml中配置如下内容: 2.在spring配置文件中,配置处理器适配器HandlerAdapter和映射器 ...