题目链接: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. Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

    题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的 ...

  2. 纯tarjan poj2186

    tarjan,叙叙旧咯 #include<cstdio>#define maxn 50005int e[maxn],ne[maxn],be[maxn],all;int DFN[maxn], ...

  3. UVa 10285 Longest Run on a Snowboard【记忆化搜索】

    题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...

  4. HDU 2544 最短路 (最短路,spfa)

    题意:中文题目 思路:spfa+SLF优化.关于SPFA的详情请戳我 #include <bits/stdc++.h> using namespace std; , INF=0x7f7f7 ...

  5. sublime3 常用功能总结

    介绍几个常见的功能: l 自动完成:自动完成的快捷键是Tab和Enter,如果在html文件中,输入cl按下tab或Enter,即可自动补全为class=””:加上zencoding后,更是如虎添翼, ...

  6. 如何使用 orachk 工具

    Oracle RAC 安装完毕后的健壮性是一个令人头疼的问题.之前Oracle为之专门推出了raccheck工具,确实方便了我们这些个苦逼的DBA.现在Oracle在raccheck的基础之上又推出了 ...

  7. centos软件环境

    1,保持能链接外网和yum的可用性. 注意:yum配置项中最好:keepcache=1 2,yum install gcc, gcc-c++, make, cmake, 3, ntfs-3g wget ...

  8. SQL SERVER 实现分组合并实现列数据拼接

    需求场景: SQL SERVER 中组织的数据结构是一个层级关系,现在需要抓出每个组织节点以上的全部组织信息,数据示例如下: ADOrg_ID--------------ParentID------- ...

  9. Delphi 712操作word

    //导出Wordprocedure TFrm_Computing.ExportWord;var wordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, ...

  10. mysql DDL语句

    sql语言分为三个级别. 1.ddl 语句 ,数据定义语句,定义了数据库.表.索引等对象的定义.常用语句包含:create.drop.alter. 2.dml 语句 ,数据操纵语句,用于添加.删除.更 ...