题目传送门

 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
13 const int MAXN = 2e3 + 10;
14 const int INF = 0x3f3f3f3f;
15 int n, m;
16 int dx[4][3] = {{1,0,1},{0,-1,-1},{-1,-1,0},{1,1,0}};
17 int dy[4][3] = {{0,1,1},{1,0,1},{0,-1,-1},{0,-1,-1}};
18 char s[MAXN][MAXN];
19
20 bool ok(int x, int y)
21 {
22     if (x < 0 || x >= n)    return false;
23     if (y < 0 || y >= m)    return false;
24
25     return true;
26 }
27
28 void BFS(void)
29 {
30     queue<pair<int, int> > Q;
31     for (int i=0; i<n; ++i)
32     {
33         for (int j=0; j<m; ++j)
34         {
35             if (s[i][j] == '.')
36             {
37                 Q.push (make_pair (i, j));
38             }
39         }
40     }
41
42     while (!Q.empty ())
43     {
44         int x = Q.front ().first;    int y = Q.front ().second;
45         Q.pop ();
46         for (int i=0; i<4; ++i)
47         {
48             int cnt = 0;    int px, py;    bool flag = true;
49             for (int j=0; j<3; ++j)
50             {
51                 int tx = x + dx[i][j];    int ty = y + dy[i][j];
52                 if (ok (tx, ty))
53                 {
54                     if (s[tx][ty] == '*')
55                     {
56                         cnt++;    px = tx;    py = ty;
57                     }
58                 }
59                 else    flag = false;
60             }
61             if (flag && cnt == 1)
62             {
63                 s[px][py] = '.';    Q.push (make_pair (px, py));
64             }
65         }
66     }
67 }
68
69 int main(void)        //Codeforces Round #297 (Div. 2) D. Arthur and Walls
70 {
71     while (scanf ("%d%d", &n, &m) == 2)
72     {
73         for (int i=0; i<n; ++i)    scanf ("%s", s[i]);
74         BFS ();
75         for (int i=0; i<n; ++i)    printf ("%s\n", s[i]);
76     }
77
78     return 0;
79 }
80
81
82 /*
83 5 5
84 .*.*.
85 *****
86 .*.*.
87 *****
88 .*.*.
89 6 7
90 ***.*.*
91 ..*.*.*
92 *.*.*.*
93 *.*.*.*
94 ..*...*
95 *******
96 */

转载于:https://www.cnblogs.com/Running-Time/p/4534492.html

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls相关推荐

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

    题目链接: http://codeforces.com/contest/525/problem/D 题意 给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最 ...

  2. 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 MB Submit: xxx  ...

  3. 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 MB Submit: xxx ...

  4. 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

    题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <al ...

  5. Codeforces Round #167 (Div. 1) C. Dima and Horses(BFS+贪心)

    题目大意 有 n(1≤n≤3*105) 匹马,每条马都有几个敌人(不超过 3 个),现在要求把这些马分成两部分(允许一部分中没有一条马),使得对于每条马,和它在同一部分中的敌人的数量不超过1个 给出了 ...

  6. Codeforces Round #636 (Div. 3) E. Weights Distributing 思维 + bfs

    传送门 文章目录 题意: 思路: 题意: n≤2e5,m≤2e5n\le2e5,m\le2e5n≤2e5,m≤2e5 思路: 怎么感觉每场div3div3div3都有一个巧妙的图论题. 首先如果只有两 ...

  7. Codeforces Round #646 (Div. 2) E(贪心,bfs)

    Codeforces Round #646 (Div. 2) E 题目大意: 给一棵树,每个节点有三个权值 A,B,C, (B,C为0或1),每次你可以花费 A[u] *k的代价让A子树中的任意 k ...

  8. Codeforces Round #709 (Div. 1) B. Playlist 链表维护 + bfs

    传送门 文章目录 题意: 思路: 题意: 思路: 紧跟刘爷脚步补题. 不难想到用链表维护下一个数是什么,这样就跟以前做过的一个题差不多了,首先将初始的时候删掉的点的前一个点即为题目中的AAA入队,让后 ...

  9. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

最新文章

  1. java使用动态代理来实现AOP(日志记录)的实例代码
  2. 微型计算机原理sar,微机原理的题一.程序分析 1.MOV AX,80F0H MOV CL,10H SAR AX,CL ADD AX,80H...
  3. 对于Mybatis在C#.Net中个人使用的总结(一) Mybatis 的结果映射
  4. python安装后在哪里找_python安装后的目录在哪里
  5. 阮一峰react demo代码研究的学习笔记 - demo4 debug - create element and Render
  6. 直流UPS的研究和应用前景
  7. 前端学习(3100):vue+element今日头条管理-react简介
  8. 使用exp导出导入,需要注意的问题。
  9. 微信支付,redirect_uri域名与后台配置不一致,错误代码10003
  10. 开源xen对比_女实习生在Xen Project上摇摆开源
  11. iPhone开发经典语录
  12. 帮写python代码_10个工具,帮你写出更好的Python代码
  13. 使用Python批量修改PPTX文件中文本框格式
  14. mysql存储过程in_在MySQL存储过程中使用WHERE IN()
  15. 第一回合:.net与 C#基本概念
  16. 让油猴脚本只执行一次
  17. win7系统数据库服务器,win7数据库 服务器
  18. 【牛客网-公司真题-前端入门篇】——如何快速上手牛客
  19. 微信红包发送关键代码
  20. 项目管理44个过程输入输出工具技术巧记法

热门文章

  1. linux 定时任务
  2. Mysql5.7中子查询时order by与group by合用无效的解决办法
  3. UVA 146 ID Codes
  4. 线段树练习——区间合并
  5. 《Python核心编程》第二版第36页第二章练习 -Python核心编程答案-自己做的-
  6. sqlserver sql语句|经典sql语句|实用sql语句
  7. Net中的反射使用入门
  8. ASP.NET 程序中常用的三十三种代码(9)
  9. 社交电商这条路,也许只有腾讯能走远
  10. 行业思考 | 酷炫动效是否利于你的产品设计?