[codeforces525D]BFS
题目大意:
给定一个包含'.'和'*'的地图,每次操作可以把'*'->'.',用最少的操作使得新图满足条件:所有的连通块为矩形('.'为可达点)
解法:
用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的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
随机推荐
- css: scroll-table
.scroll-table { table tbody { display: block; max-height: 120px; overflow-y: scroll; } table thead,t ...
- 15分钟从零开始搭建支持10w+用户的生产环境(三)
上一篇文章介绍了这个架构中,选择MongoDB做为数据库的原因,及相关的安装操作. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(二) 三.WebServer 在SOA和gRPC大行其 ...
- 易学又实用的新特性:for...of
今天带来的知识点既简单又使用,是不是感觉非常的棒啊,OK,不多说了,咱们开始往下看. for...of 是什么 for...of 一种用于遍历数据结构的方法.它可遍历的对象包括数组,对象,字符串,se ...
- Jenkins(2)- 更改插件源为国内源
如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html jenkins插件清华大学镜像地址 ...
- SourceTree for Windows跳过登录解决方法
来源:https://blog.csdn.net/t_332741160/article/details/79611303 SourceTree 是一个强大的git管理客户端,但是在使用最新版需要登录 ...
- 解释BOM头和去掉方法
http://www.thinkphp.cn/topic/2592.html 以上是叫你去掉bom头的,因为有些文件加载不出来就是window会以记事本的形式打开,然后默认给我们加了了bom头,有些文 ...
- 矩阵类的代码(C++)
The Codes of Matrix Class Matrix.h:#ifndef MATRIX_H#define MATRIX_H #include<iostream> #includ ...
- Spring5参考指南:JSR 330标准注解
文章目录 @Inject 和 @Named @Named 和 @ManagedBean 之前的文章我们有讲过,从Spring3.0之后,除了Spring自带的注解,我们也可以使用JSR330的标准注解 ...
- 谈谈Java的线程池设计
在实际项目中,如果因为想异步执行暂时性的任务而不断创建线程是很浪费资源的事情(当一个任务执行完后,线程也没用了).这种情况下,最好是将任务提交给线程池执行. 所谓池,就是将管理某一种资源,对资源进行复 ...
- Shiro(三):Spring-boot如何集成Shiro(下)
上一篇文章介绍了shiro在spring-boot中通过filter实现authentication流程(通过设置filterMaps也可以达到authorization的目的):这篇文章主要介绍sp ...