题目链接: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. IE css expression(表达式)

    很多时候我们需要对IE6的bug写一些hack,如max-height,absolute元素高度100%等. css里面的 expression(表达式)和js里面的差不多,如: 获取当前元素的高度: ...

  2. 百度HTTPS加密搜索有什么用?

    前段时间,我曾提到百度支持移动端HTTPS SSL加密搜索,用以保护用户隐私.最近,百度开始支持PC端HTTPS SSL加密搜索,现在可以启用 https://www.baidu.com 搜索.我很少 ...

  3. Java 基础之认识 Annotation

    Java 基础之认识 Annotation 从 JDK 1.5 版本开始,Java 语言提供了通用的 Annotation 功能,允许开发者定义和使用自己的 Annotation 类型.Annotat ...

  4. poj 1699 Best Sequence (搜索技巧 剪枝 dfs)

    题目链接 题意:给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因. 分析:先计算出add数组,再dfs枚举. 空间复杂度O(n*n),  最坏时间复杂度 ...

  5. bzoj3632

    裸的最大团,随机化大法好 多次随机出一个选择顺序然后贪心即可 ..,..] of boolean; a:..] of longint; v:..] of boolean; n,m,i,j,x,y,an ...

  6. C# 创建iis站点以及IIS站点属性,iis不能启动站点

    DontLog = False是否将客户端的请求写入日志文件 2011年04月09日 #region CreateWebsite 新增网站 public string CreateWebSite(st ...

  7. 根据中国气象局提供的API接口实现天气查询

    中国气象局提供了三个天气查询的API接口: [1]http://www.weather.com.cn/data/sk/101190101.html [2]http://www.weather.com. ...

  8. TS数据结构分析

    1.TS包得数据结构 2. // Transport packet headertypedef struct TS_packet_header{    unsigned sync_byte       ...

  9. Entity Framework中编辑时错误ObjectStateManager 中已存在具有同一键的对象

    ObjectStateManager 中已存在具有同一键的对象.ObjectStateManager 无法跟踪具有相同键的多个对象. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈 ...

  10. HTML5实现扫描识别二维码/生成二维码

    扫描识别二维码 思路: 1. 操作摄像头,获取图片.HTML5 WEBRTC的navigator.getUserMedia方法去实时获取摄像头资源.  2. 利用canvas使用相关算法分析图片识别图 ...