题目链接:http://codeforces.com/problemset/problem/598/D

题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能看到的壁画有多少(大概这样吧)。

我的做法是bfs(dfs也可以)这个为'.'的点,要是遇到上下左右其中有'*'的话就加起来。要是每次询问然后bfs一下肯定超时,所以我用一个ans[1005][1005]记录每个点的值,ok[1005][1005]数组判断是否访问过,初始化为false。然后开始遍历一次,遇到'*'的ans则为0,遇到'.' 且 ok[i][j] = false的就bfs周围相连的'.',算出答案然后再逐个赋值给一个房间的ans[i][j],都标记经过 即ok[i][j] = true ...。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std;
char map[MAXN][MAXN];
bool ok[MAXN][MAXN];
int ans[MAXN][MAXN];
int n , m , res , tx[] = {- , , , } , ty[] = { , , , -};
struct data {
int x , y;
}; void init() {
memset(ok , false , sizeof(ok));
} bool judge(int x , int y) { //判断点是否是'.' 且未经过
if(x < n && y < m && x >= && y >= && map[x][y] != '*' && !ok[x][y]) {
return true;
}
return false;
} bool judge2(int x , int y) { //判断点是否是'*'
if(x < n && y < m && x >= && y >= && map[x][y] == '*') {
return true;
}
return false;
} int get(int x , int y) { //壁画的相加
int num = ;
for(int i = ; i < ; i++) {
if(judge2(x + tx[i] , y + ty[i])) {
num++;
}
}
return num;
} void bfs(int x , int y) {
vector<data > v; //用来存相连的'.'
queue <data> Q;
data a;
a.x = x , a.y = y;
Q.push(a);
while(!Q.empty()) {
data temp = Q.front();
v.push_back(temp);
Q.pop();
if(map[temp.x][temp.y] == '.') {
res += get(temp.x , temp.y);
ok[temp.x][temp.y] = true;
}
for(int i = ; i < ; i++) {
a.x = temp.x + tx[i] , a.y = temp.y + ty[i];
if(judge(a.x , a.y)) {
Q.push(a);
ok[a.x][a.y] = true;
}
}
}
for(int i = ; i < v.size() ; i++) {
ans[v[i].x][v[i].y] = res;
}
v.clear(); //清空
} int main()
{
int q , sx , sy;
ios::sync_with_stdio(false);
while(cin >> n >> m >> q) {
memset(ans , , sizeof(ans));
for(int i = ; i < n ; i++)
cin >> map[i];
init();
for(int i = ; i < n ; i++) {
for(int j = ; j < m ; j++) {
res = ;
if(!ok[i][j] && map[i][j] == '.') {
bfs(i , j);
}
}
}
while(q--) {
cin >> sx >> sy;
cout << ans[sx - ][sy - ] << endl;
}
}
}

Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)的更多相关文章

  1. 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 ...

  2. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  6. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  8. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. Mac系统在终端中查看CPU信息的命令

    在mac os x的终端中以命令行的形式查看本机cpu信息: sysctl -n machdep.cpu.brand_string E.G. lis-mbp:Home jenkins$ sysctl ...

  2. 一个简单的ORM制作(SQL帮助类)

    一个简单的ORM制作大概需要以下几个类: SQL执行类 CURD操作类 其他酱油类 先从SQL执行类说起,可能会涉及数据库的迁移等问题,所以需要定义一个接口以方便迁移到其他数据库, 事务没提供命名,若 ...

  3. UVa 10294 (Pólya计数) Arif in Dhaka (First Love Part 2)

    Burnside定理:若一个着色方案s经过置换f后不变,称s为f的不动点,将置换f的不动点的数目记作C(f).等价类的数目等于所有C(f)的平均值. 一个项链,一个手镯,区别在于一个能翻转一个不能,用 ...

  4. DirectShow系统初级指南

    流媒体的处理,以其复杂性和技术性,一向广受工业界的关注.特别伴随着因特网的普及,流媒体在网络上的广泛应用,怎样使流媒体的处理变得简单而富有成效逐渐成为了焦点问题.选择一种合适的应用方案,事半功倍.此时 ...

  5. 【转】Android Studio系列教程一--下载与安装

    原文网址:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 背景 相信大家对Android Studio已经不陌生 ...

  6. hdu 2059(dp)

    题意:容易理解... 思路:dp[i]表示乌龟到达第i个充电站时最少花费时间到第 i 个充电站后,从起点开始遍历到第 i-1 个充电站,得到最少花费时间 状态转移方程:dp[i]=min(dp[j]+ ...

  7. Hadoop集群中Hbase的介绍、安装、使用

    导读 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. 一.Hbase ...

  8. PHP学习笔记--文件目录操作(文件上传实例)

    文件操作是每个语言必须有的,不仅仅局限于PHP,这里我们就仅用PHP进行讲解 php的文件高级操作和文件上传实例我放在文章的最后部分.--以后我还会给大家写一个PHP类似于网盘操作的例子 注意:阅读此 ...

  9. codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)

    题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...

  10. 【剑指offer 面试题15】链表中倒数第K个结点

    思路: 定义两个指针同时指向head,第一个指针先走K-1步,随后二个指针同时移动,当第一个指针到末尾处时,第二个指针所指向的即为倒数第K个结点. #include <iostream> ...