[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 ...
随机推荐
- tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor
最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...
- Xshell 设置右键粘贴即是复制
打开工具->选项->键盘和鼠标面板 1.鼠标部分的右击设置"粘贴剪切板的内容". 2.选择部分,在"自动将所选文本复制到剪切板"前打勾
- Django中HttpRequest常用参数介绍
HttpRequest对象常用参数介绍,以及前端不同请求方式(http方法/Content-Type类型)对应的参数获取方式. 一.HttpRequest对象 django请求对象的详细参数以及实现方 ...
- 华为鲲鹏服务器安装 k3s+rancher
华为鲲鹏服务器安装 k3s+rancher 华为鲲鹏服务器 华为鲲鹏服务器采用华为自研cpu ARMv8架构,提供 Windows 和多个Linux 系统,作为服务器使用我一直使用Centos系统(不 ...
- mysql参数max_binlog_cache_size设置不当引发的血案
日常运维中的坑真是防不胜防,不一小心就遇到别人给你挖的坑.最近又遇到经验不足的DBA不知道从哪拷贝的配置文件(据说是当时参加某培训机构视频培训是资料里的模板,真的是误人子弟呀),其中把max_binl ...
- 2018版移动端ui规范
计规范是一种将移动端常用控件标准化.统一化的的文档 今天整理了一篇设计规范的文章概论,讲诉中会以ios做介绍,安卓由于开源,平台相对教多不做单一阐述,实际操作的时候,我们不管是做一代还是二次的迭代产品 ...
- git .gitignore不生效
原因是.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的. 解决方法: 1.先把规则写好,然后把规则对应的文件删了,然后 ...
- js 实现动画功能,完整解析插件版 可更改配置参数[animate.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS写一个运动插件 基本功能: 补充 ...
- Json & pickle 数据序列化
前提: 文本文件中只能写入字符串或ascii码格式的内容. info={'name':'zoe','age':18} f=open('test.txt','w') f.write(info) #在文本 ...
- 【Linux常见命令】date命令
Linux date命令:可以用来显示或设定系统的日期与时间. 在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中可用的标记列表如下: 时间方面: %H : 小时(00..2 ...