CodeForces 525D Arthur and Walls
广搜。看了官方题解才会的.....
定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块。
首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳过
改完之后,检查周围8个点是否是元素块,如果有新产生的元素块,那么压入队列。
这样操作完之后就是答案。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; struct Node
{
int a,b;
Node(int A,int B){a=A,b=B;}
};
queue<Node>Q;
const int N=;
int n, m;
char g[N][N]; bool check(int x, int y)
{
if (g[x][y] == '.' || x < || y < || x >= n || y >= m)return ;
if (g[x][y - ] == '.'&&g[x - ][y - ] == '.'&&g[x - ][y] == '.')return ;
if (g[x - ][y] == '.'&&g[x - ][y + ] == '.'&&g[x][y + ] == '.')return ;
if (g[x][y + ] == '.'&&g[x + ][y + ] == '.'&&g[x + ][y] == '.')return ;
if (g[x][y - ] == '.'&&g[x + ][y - ] == '.'&&g[x + ][y] == '.')return ;
return ;
} void bfs()
{
for(int i=;i<n;i++)
for(int j=;j<m;j++) if(check(i,j)) Q.push(Node(i,j)); while(!Q.empty())
{
Node h=Q.front(); Q.pop();
if(!check(h.a,h.b)) continue;
g[h.a][h.b]='.'; for(int i=-;i<=;i++)
{
for(int j=-;j<=;j++)
{
if(i==&&j==) continue;
if(check(h.a+i,h.b+j)) Q.push(Node(h.a+i,h.b+j));
}
}
}
} int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = ; i < n; i++) scanf("%s", g[i]);
bfs();
for(int i=;i<n;i++) printf("%s\n",g[i]);
}
return ;
}
CodeForces 525D Arthur and Walls的更多相关文章
- Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- codeforces D - Arthur and Walls
这题说的是给了一个矩阵,必须让.连接起来的图形变成矩形,2000*2000的矩形,那么我们就可以知道了,只要是存在一个有点的区域应该尽量将他削为矩形,那么将这个图形进行缩放,最后我们知道只要存在一个2 ...
- cf 525D.Arthur and Walls
判断2*2的正方形,是不是3个"."1个"*"然后暴力bfs就好.(这种处理也是挺神奇的2333%%题解) #include<bits/stdc++.h& ...
- Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
- [BFS,大水题] Codeforces 198B Jumping on Walls
题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
随机推荐
- Shell grep 和正则
在grep中正则有三种模式, 分别是基础(-G), 扩展(-E)和Perl(-P)模式 basic 模式 不支持\d, 支持\w \s \b. ?, +, {, |, (, and ) 在basic ...
- Mozilla Firefox的各级版本链接
Mozilla Firefox的各级版本链接 及语言 https://ftp.mozilla.org/pub/firefox/releases/
- STM32F407的串口采用DMA收发数据
源:STM32F407的串口采用DMA收发数据
- iOS 自定义导航栏笔记
一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC ...
- HDU 5773 The All-purpose Zero
这题想了1个多小时想不出来...方法真是精妙... 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的.因此我们可以把0拿出来,对剩下 的做O(nlogn)的LIS, ...
- CodeForces 753C Interactive Bulls and Cows (Hard)
题意:... 析:随机判断就即可,每次把不正确的删除,经过几次后就基本剩不下了. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...
- weaver_oa
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 第二十六节,Python内置函数
Python3.0内置函数 abs() 取数字的绝对值,也就是无论是正数还是负数取它的绝对值格式:abs(目标变量)返回:数字类型 #!/usr/bin/env python # -*- coding ...
- 依赖注入容器Autofac与MVC集成
Autofac是应用于.Net平台的依赖注入(DI,Dependency Injection)容器,具有贴近.契合C#语言的特点.随着应用系统的日益庞大与复杂,使用Autofac容器来管理组件之间的关 ...
- Git从远程库克隆
上次我们讲了先有本地库,后有远程库,如何关联远程库. 现在,假设我们从零开始开发,那么最好的方式就是先创建远程库,然后从远程库克隆. 首先,登录GitHub,创建一个新的仓库,gitskill 创建过 ...