题目传送门

  1. /*
  2. 题意:问最少替换'*'为'.',使得'.'连通的都是矩形
  3. BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找
  4. 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉
  5. */
  6. #include <cstdio>
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <cstring>
  10. #include <queue>
  11. using namespace std;
  12. const int MAXN = 2e3 + ;
  13. const int INF = 0x3f3f3f3f;
  14. int n, m;
  15. int dx[][] = {{,,},{,-,-},{-,-,},{,,}};
  16. int dy[][] = {{,,},{,,},{,-,-},{,-,-}};
  17. char s[MAXN][MAXN];
  18. bool ok(int x, int y)
  19. {
  20. if (x < || x >= n) return false;
  21. if (y < || y >= m) return false;
  22. return true;
  23. }
  24. void BFS(void)
  25. {
  26. queue<pair<int, int> > Q;
  27. for (int i=; i<n; ++i)
  28. {
  29. for (int j=; j<m; ++j)
  30. {
  31. if (s[i][j] == '.')
  32. {
  33. Q.push (make_pair (i, j));
  34. }
  35. }
  36. }
  37. while (!Q.empty ())
  38. {
  39. int x = Q.front ().first; int y = Q.front ().second;
  40. Q.pop ();
  41. for (int i=; i<; ++i)
  42. {
  43. int cnt = ; int px, py; bool flag = true;
  44. for (int j=; j<; ++j)
  45. {
  46. int tx = x + dx[i][j]; int ty = y + dy[i][j];
  47. if (ok (tx, ty))
  48. {
  49. if (s[tx][ty] == '*')
  50. {
  51. cnt++; px = tx; py = ty;
  52. }
  53. }
  54. else flag = false;
  55. }
  56. if (flag && cnt == )
  57. {
  58. s[px][py] = '.'; Q.push (make_pair (px, py));
  59. }
  60. }
  61. }
  62. }
  63. int main(void) //Codeforces Round #297 (Div. 2) D. Arthur and Walls
  64. {
  65. while (scanf ("%d%d", &n, &m) == )
  66. {
  67. for (int i=; i<n; ++i) scanf ("%s", s[i]);
  68. BFS ();
  69. for (int i=; i<n; ++i) printf ("%s\n", s[i]);
  70. }
  71. return ;
  72. }
  73. /*
  74. 5 5
  75. .*.*.
  76. *****
  77. .*.*.
  78. *****
  79. .*.*.
  80. 6 7
  81. ***.*.*
  82. ..*.*.*
  83. *.*.*.*
  84. *.*.*.*
  85. ..*...*
  86. *******
  87. */

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls的更多相关文章

  1. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  2. Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]

    传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...

  3. Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)

    D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  4. Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索

    Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx  ...

  5. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  7. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  8. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  9. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

随机推荐

  1. Linux主要命令

    pwd  查看当前路径 cd ..    表示后一级目录级 cd .   表示当前目录 cd ../..   后退两级 cd  表示进入当前家目录 date 返回当前的一个具体时间    -s  修改 ...

  2. html-基本form元素---ShinePans

    <html> <meta http-equiv="content-type" content="text/html;charset=UTF-8" ...

  3. Python 003- 小知识汇总(更新中)

    #查询key是否存在,可以在使用未知的字典的时候使用 #-*- coding:utf-8 -*- D={'a':1,'c':3,'b':2} for key in sorted(D): print(k ...

  4. TreeSet实现Comparator接口的排序算法的分析

    为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...

  5. hadoop reduce 阶段遍历 Iterable 的 2 个“坑”

    01 package com.test; 02   03 import java.util.ArrayList; 04 import java.util.Iterator; 05 import jav ...

  6. Shell hook

    [目的]实现 ll -as   +hook+     clear Shell脚本及钩子 - CSDN博客 https://blog.csdn.net/shengzhu1/article/details ...

  7. LOJ#139. 树链剖分

    LOJ#139. 树链剖分 题目描述 这是一道模板题. 给定一棵$n$个节点的树,初始时该树的根为 1 号节点,每个节点有一个给定的权值.下面依次进行 m 个操作,操作分为如下五种类型: 换根:将一个 ...

  8. 百度dureos CMake Error

    CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, ...

  9. 对 block 内存管理的一些看法

    首先交代一下retain cycle ,和 产生retain cycle后我们应该怎么处理. 1.retain cycle在block中是极易产生,block就是一段可以灵活使用的代码,你可以把它当做 ...

  10. Spring Boot 访问静态资源

    方法1一: 在resources目录下建立static的目录,将静态资源放到此处,可以直接访问 访问:127.0.0.1:9010/img/123.png