题目链接: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[] = {- , , , } , ty[] = { , , , -};
  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 >= && y >= && 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 >= && y >= && map[x][y] == '*') {
  29. return true;
  30. }
  31. return false;
  32. }
  33.  
  34. int get(int x , int y) { //壁画的相加
  35. int num = ;
  36. for(int i = ; i < ; 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 = ; i < ; 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 = ; 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 , , sizeof(ans));
  78. for(int i = ; i < n ; i++)
  79. cin >> map[i];
  80. init();
  81. for(int i = ; i < n ; i++) {
  82. for(int j = ; j < m ; j++) {
  83. res = ;
  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 - ][sy - ] << endl;
  92. }
  93. }
  94. }

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. apache启动报错(98)Address already in use: make_sock: could not bind to...

    # /etc/init.d/httpd startStarting httpd: (98)Address already in use: make_sock: could not bind to ad ...

  2. struts2 获取前台表单的值?? 原理??

    struts2中,在ACTION中申明一个变量 private string 变量名:然后设置变量名 的get/set方法: 在运行的时候struts2会自动获取. 比如:jsp 页面中有个文本框&l ...

  3. cocos2dx场景切换中init、onEnter、onEnterTransitionDidFinish的调用顺序

    这些方法调用的先后顺序如下(使用 replaceScene 方法): 1. 第2个场景的 scene 方法 2. 第2个场景的 init 方法 3. 第2个场景的 onEnter 方法 4. 转场 5 ...

  4. 业界最具影响力MySQL精品文章荟萃(300篇)

    MySQL是一种关联数据库管理系统,SQL语言是用于访问数据库的最常用标准化语言.本文档收集的资料有MySQL数据库备份与恢复,配置,解决方案等,供大家方便统一阅读. 博客专题 1     MySQL ...

  5. Vagrant搭建Ubuntu-JavaEE开发环境——Tomcat+JDK+MySQL+dubbo+测试

    Vagrant搭建(Tomcat8+JDK7+MySQL5+dubbo) JDK 1.下载jdk 2.解压JDK tar -xzvf jdk-7u79-linux-x64.tar.gz 3.设置环境变 ...

  6. 【又见LCS】NYOJ-37 回文字符串

    [题目链接] 回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...

  7. 【转】【Android】对话框 AlertDialog -- 不错不错

    原文网址:http://blog.csdn.net/feng88724/article/details/6171450 本讲介绍一下Android基本组件:对话框AlertDialog. API: j ...

  8. GreenDao官方文档翻译(上)

    笔记摘要: 上一篇博客简单介绍了SQLite和GreenDao的比较,后来说要详细介绍下GreenDao的使用,这里就贴出本人自己根据官网的文档进行翻译的文章,这里将所有的文档分成上下两部分翻译,只为 ...

  9. JTA事务管理--配置剖析

    概述    [IT168 专稿]Spring 通过AOP技术可以让我们在脱离EJB的情况下享受声明式事务的丰盛大餐,脱离Java EE应用服务器使用声明式事务的道路已经畅通无阻.但是很大部分人都还认为 ...

  10. Mahout踩坑之路

    一.版本对比 公司版Mahout 由于Mahout只能允许于hadoop0.20以上版本上,而百度的hadoop是hadoop0.19的一个分支.因此百度HPC组曾经将Mahout移植到百度的hado ...