题目链接: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 ...。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6
 7 using namespace std;
 8 char map[MAXN][MAXN];
 9 bool ok[MAXN][MAXN];
10 int ans[MAXN][MAXN];
11 int n , m , res , tx[] = {-1 , 0 , 1 , 0} , ty[] = {0 , 1 , 0 , -1};
12 struct data {
13     int x , y;
14 };
15
16 void init() {
17     memset(ok , false , sizeof(ok));
18 }
19
20 bool judge(int x , int y) {              //判断点是否是'.'   且未经过
21     if(x < n && y < m && x >= 0 && y >= 0 && map[x][y] != '*' && !ok[x][y]) {
22         return true;
23     }
24     return false;
25 }
26
27 bool judge2(int x , int y) {            //判断点是否是'*'
28     if(x < n && y < m && x >= 0 && y >= 0 && map[x][y] == '*') {
29         return true;
30     }
31     return false;
32 }
33
34 int get(int x , int y) {     //壁画的相加
35     int num = 0;
36     for(int i = 0 ; i < 4 ; i++) {
37         if(judge2(x + tx[i] , y + ty[i])) {
38             num++;
39         }
40     }
41     return num;
42 }
43
44 void bfs(int x , int y) {
45     vector<data > v;    //用来存相连的'.'
46     queue <data> Q;
47     data a;
48     a.x = x , a.y = y;
49     Q.push(a);
50     while(!Q.empty()) {
51         data temp = Q.front();
52         v.push_back(temp);
53         Q.pop();
54         if(map[temp.x][temp.y] == '.') {
55             res += get(temp.x , temp.y);
56             ok[temp.x][temp.y] = true;
57         }
58         for(int i = 0 ; i < 4 ; i++) {
59             a.x = temp.x + tx[i] , a.y = temp.y + ty[i];
60             if(judge(a.x , a.y)) {
61                 Q.push(a);
62                 ok[a.x][a.y] = true;
63             }
64         }
65     }
66     for(int i = 0 ; i < v.size() ; i++) {
67         ans[v[i].x][v[i].y] = res;
68     }
69     v.clear();  //清空
70 }
71
72 int main()
73 {
74     int q , sx , sy;
75     ios::sync_with_stdio(false);
76     while(cin >> n >> m >> q) {
77         memset(ans , 0 , sizeof(ans));
78         for(int i = 0 ; i < n ; i++)
79             cin >> map[i];
80         init();
81         for(int i = 0 ; i < n ; i++) {
82             for(int j = 0 ; j < m ; j++) {
83                 res = 0;
84                 if(!ok[i][j] && map[i][j] == '.') {
85                     bfs(i , j);
86                 }
87             }
88         }
89         while(q--) {
90             cin >> sx >> sy;
91             cout << ans[sx - 1][sy - 1] << endl;
92         }
93     }
94 }

转载于:https://www.cnblogs.com/Recoder/p/4965879.html

Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)相关推荐

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

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

  2. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  3. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  4. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  5. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

  6. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  7. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

  8. Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs

    传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici​个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...

  9. Educational Codeforces Round 17 E. Radio stations cdq分治 + 树状数组

    传送门 文章目录 题意 思路: 题意 有nnn个电台,对于每个电台iii有三个参数xi,ri,fix_i,r_i,f_ixi​,ri​,fi​,分别指他们的坐标.作用半径.频率.如果两个电台频率差值在 ...

最新文章

  1. 计算机专业美国最好的学校排名,美国计算机专业大学排名
  2. java 实体比较_java比较实体类
  3. 又是一年毕业高峰期,分享下我的2年工作经验
  4. 网络规划设计师论文考察要点
  5. 调试maven源代码
  6. java 安全认证_restful安全认证
  7. JAVA标准输出错误输出,从tsls输出中提取标准错误
  8. https://127.0.0.1:8080/test?param={%22..报错
  9. Elasticsearch一些常用操作和一些基础概念
  10. 《统计学习方法》——感知机
  11. md5解密 python_python写一个md5解密器示例
  12. 分布式缓存Redis使用以及原理
  13. 拥塞控制,图文并茂(挺丰富,借鉴较多大佬的思想)
  14. FreeRTOS(教程非常详细)
  15. 土方工程量计算表格excel_土方方格网计算表格excel.xls
  16. win10修改dns服务器地址,教你Win10如何更改首选DNS服务器地址
  17. 年度最大促销,这家“娃界小米”要在双十一发大招
  18. 俄罗斯方块的设计思路
  19. DMA+PWM驱动彩色RGB灯
  20. mysql 快速入门,SOAR 101 快速入门指南

热门文章

  1. 词嵌入和网络在NLP中贡献
  2. apache php 工作模式,PHP Apache中两种工作方式区别(CGI模式、Apache 模块DLL)
  3. h3c trunk口改access_H3C交换机二层应用及三层交换基本配置
  4. libtorch和torchvision的编译安装
  5. java string.substring 参数,Java,String类中的subString()方法,stringsubstring
  6. 2020年浙江高考考python吗_2020年浙江高考改革最新消息 浙江新高考规则解读
  7. .net core json 为null输出_SpringBoot实战(九):标准化json返回值
  8. 【模板】高精度 [高精度]
  9. Elasticsearch-搜索推荐
  10. Android签名机制---签名过程