题目大意:

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

解法:

用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. linux常用命令--文件的权限

    ls -lh 显示权限 ls /tmp | pr -T5 -W$COLUMNS 将终端划分成5栏显示 chmod ugo+rwx directory1 设置目录的所有人(u).群组(g)以及其他人(o ...

  2. 3. pkg

    程序打包成可执行文件(.exe) 1.)  npm install -g pkg 2.)  单个文件:pkg entrance.js ( windows: pkg -t win entrance.js ...

  3. [De1CTF 2019]SSRF Me

    原帖地址 : https://xz.aliyun.com/t/5927 De1CTF 2019 的一个题目总结 题目描述 直接查看页面源代码可以看到正确格式的代码 #! /usr/bin/env py ...

  4. response没有实现跳转,而是提示浏览器下载文件

    问题简述: web项目中,response没能实现重定向跳转网页,而是通知浏览器下载文件. 代码如下: response.getWriter().write("<h1 style='c ...

  5. Tensorflow 模型线上部署

    获取源码,请移步笔者的github: tensorflow-serving-tutorial 由于python的灵活性和完备的生态库,使得其成为实现.验证ML算法的不二之选.但是工业界要将模型部署到生 ...

  6. df卡住的解决办法

    在使用网络存储时,如果网络存储出问题.比如使用NFS,网络中断,df -h会卡住 情形一 ctrl+c是能取消中断的,这种情况算是比较幸运.使用mount查看有哪些挂载点,将其卸载即可. 情形二 ct ...

  7. 机器学习5- 对数几率回归+Python实现

    目录 1. 对数几率回归 1.1 求解 ω 和 b 2. 对数几率回归进行垃圾邮件分类 2.1 垃圾邮件分类 2.2 模型评估 混淆举证 精度 交叉验证精度 准确率召回率 F1 度量 ROC AUC ...

  8. 从零开始学习docker之在docker中搭建redis(单机)

    docker搭建redis 一.环境准备 云环境:CentOS 7.6 64位 二.下载镜像 从docker hub中找到redis镜像 传送门------https://hub.docker.com ...

  9. mysql 之 清空表中数据

    清空表的时候注意外键约束 命令版 查询数据库中所有表名select table_name from information_schema.tables where table_schema='DB_n ...

  10. 解决material UI中弹窗(dialog、popover等)内容被遮挡问题

    在material ui中有几种弹出层,比如:dialog.popover等,这些弹出层都会遇到的一个公共问题是: 假如弹出层中的内容变化了,弹出层的位置并不会重新定位. 这样,假如一开始弹出层定位在 ...