题目大意:

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

解法:

用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. Redis学习与应用-位图

    什么是位图 位图bitmap是通过一个bit来表示某个元素对应的值或者状态,是由一组bit位组成,每个bit位对应0和1两个状态,虽然内部还是采用string类型进行存储,但是redis提供了直接操作 ...

  2. 不停机还能替换代码?6年的 Java程序员表示不可思议

    相信很多人都有这样一种感受,自己写的代码在开发.测试环境跑的稳得一笔,可一到线上就抽风,不是缺这个就是少那个反正就是一顿报错,而线上调试代码又很麻烦,让人头疼得很.不过, 阿里巴巴出了一款名叫Arth ...

  3. windows下部署.netcore+docker系列三 (unbuntu 18.4 下安装ftp)

    // 先更新下系统sudo apt-get update//安装ftpsudo apt-get install vsftpd// 启动 服务sudo service vsftpd start//ftp ...

  4. prefetch 和 preload 及 webpack 的相关处理

    使用预取和预加载是网站性能和用户体验提升的一个很好的途径,本文介绍了使用 prefetch 和 prefetch 进行预取和预加载的方法,并使用 webpack 进行实现 Link 的链接类型 < ...

  5. UDO、TCP、HTTP、websocket

    如图:

  6. 我们常听到的WAL到底是什么

    什么是 WAL WAL(Write Ahead Log)预写日志,是数据库系统中常见的一种手段,用于保证数据操作的原子性和持久性. 在计算机科学中,预写式日志(Write-ahead logging, ...

  7. PL/SQL 九九乘法表

    和shell脚本九九乘法表一样,只是语法有少出入 先看看效果图先: 利用for循环: SET SERVEROUTPUT ON DECLARE x INT :=1; y INT :=1; BEGIN F ...

  8. [Qt] 打开Diskmgmt

    QProcess mOpenDiskMgmt; QString program = "cmd"; QStringList arguments; arguments << ...

  9. (c++ std) 查找 vector 中的元素

    You can use std::find from <algorithm>: std::find(vector.begin(), vector.end(), item) != vecto ...

  10. Spring Security Oauth2 使用 token 访问资源服务器出现异常:Invalid token does not contain resource id (oauth2)

    异常如图 查看资源服务器的日志 p.a.OAuth2AuthenticationProcessingFilter : Authentication request failed: error=&quo ...