题目大意:

给定一个包含'.'和'*'的地图,每次操作可以把'*'->'.',用最少的操作使得新图满足条件:所有的连通块为矩形('.'为可达点)

解法:

用bfs来模拟操作的过程,对于一个2*2的块,如果只有一个‘*’,那么这个'*'是肯定要被变为'.',于是又可能影响这个点周围相邻的点,一开始把所有满足这个形式的点加入队列,考虑所有周围的点,如果有新的点满足这个形式,则加入队列。代码如下

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define Rep(a, b) for(int a = 0; a < b; a++)
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} typedef double db;
typedef long long LL;
typedef pair<int, int> pii; const int dx[] = {, , -, , , , -, -};
const int dy[] = {, -, , , -, , , -};
const int maxn = 1e6 + ;
const int maxm = 1e5 + ;
const int MD = 1e9 +;
const int INF = 1e9 + ; queue<pii> Q; char s[][]; int n, m; bool ok(int x, int y) {
return x >= && x < n && y >= && y < m && s[x][y] == '.';
} bool chk(int x, int y) {
if (s[x][y] == '.') return false;
int d[] = {-, , -, , , , , , , , , -, , -, -, -, -, };
bool t[];
for (int i = ; i < ; i += ) {
if (ok(x + d[i], y + d[i + ])) {
t[i >> ] = true;
}
else {
t[i >> ] = false;
}
}
for (int i = ; i < ; i += ) {
if (t[i] && t[i + ] && t[i + ]) {
return true;
}
}
return false;
} int main() {
//freopen("in.txt", "r", stdin);
cin >> n >> m;
for (int i = ; i < n; i++) {
scanf("%s", s[i]);
}
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if (chk(i, j)) {
Q.push(make_pair(i, j));
s[i][j] = '.';
}
}
}
while (!Q.empty()) {
pii H = Q.front(); Q.pop();
for (int i = ; i < ; i++) {
int xx = H.first + dx[i], yy = H.second + dy[i];
if (chk(xx, yy)) {
Q.push(make_pair(xx, yy));
s[xx][yy] = '.';
}
}
}
for (int i = ; i < n; i++) {
puts(s[i]);
}
return ;
}

[codeforces525D]BFS的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. windows编译动态链接库,dll+lib的形式

    之前一直在linux上做开发,没怎么关注过windows上如何编译动态链接库.不过一直存疑,为什么windows上的动态链接库是.dll配合.lib使用的,这个又是怎么生成的呢,通过一段时间的查资料和 ...

  2. PHP出现SSL certificate:unable to get local issuer certificate的解决办法

    当本地curl需要访问https时,如果没有配置证书,会出现SSL certificate: unable to get local issuer certificate错误信息. 解决办法: 1.下 ...

  3. [Abp vNext 入坑分享] - 3.简单的用户模块功能开发

    一.简要说明 本篇文章开始进行业务模块的开发模拟,借助user模块来进行业务开发,主要是用户相关的基础操作.主要是先使用Users来体验整个开发的流程.主要是先把一个基础流程跑顺利,在这里我并不会过于 ...

  4. Java的自动装箱

    JDK5的新特性自动装箱:把基本类型转换为包装类类型自动拆箱:把包装类类型转换为基本类型 注意一个小问题: 在使用时,Integer x = null;代码就会出现NullPointerExcepti ...

  5. tensorflow1.0 dropout层

    """ Please note, this code is only for python 3+. If you are using python 2+, please ...

  6. 十分钟搞懂Elasticsearch数字搜索原理

    更多精彩内容请看我的个人博客或者扫描二维码,关注微信公众号:佛西先森 前言 Elasticsearch诞生的本意是为了解决文本搜索太慢的问题,ES会默认将所有的输入内容当作字符串来理解,对于字段类型是 ...

  7. Xtrabackup全量 增量备份详解

    xtrabackup是Percona公司CTO Vadim参与开发的一款基于InnoDB的在线热备工具,具有开源,免费,支持在线热备,备份恢复速度快,占用磁盘空间小等特点,并且支持不同情况下的多种备份 ...

  8. docker中安装nginx,部署前端代码

    最近在学习docker,初次接触,难免遇到磕磕碰碰,遂将其整理成博客,以便日后查看. 1.拉取nginx镜像 直接从官方镜像库拉取简单粗暴: docker pull nginx 2.运行 docker ...

  9. 你的GitHub,怎么和我用的不太一样?

    说起代码托管,相信绝大多数人脑海中浮现出的第一个词都是"GitHub".经过多年的发展,GitHub俨然已经成为了代码托管领域的标签- 随着国内互联网环境的优化,互联网产业链的不断 ...

  10. Condition的await()和signal()流程

    介绍 Condition是j.u.c包下提供的一个接口. 可以翻译成 条件对象,其作用是线程先等待,当外部满足某一条件时,在通过条件对象唤醒等待的线程.ArrayBlockingQueue就是通过Co ...