BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
/*
题意:问最少替换'*'为'.',使得'.'连通的都是矩形
BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找
在2*2的方格里,若只有一个是'*',那么它一定要被替换掉
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std; const int MAXN = 2e3 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int dx[][] = {{,,},{,-,-},{-,-,},{,,}};
int dy[][] = {{,,},{,,},{,-,-},{,-,-}};
char s[MAXN][MAXN]; bool ok(int x, int y)
{
if (x < || x >= n) return false;
if (y < || y >= m) return false; return true;
} void BFS(void)
{
queue<pair<int, int> > Q;
for (int i=; i<n; ++i)
{
for (int j=; j<m; ++j)
{
if (s[i][j] == '.')
{
Q.push (make_pair (i, j));
}
}
} while (!Q.empty ())
{
int x = Q.front ().first; int y = Q.front ().second;
Q.pop ();
for (int i=; i<; ++i)
{
int cnt = ; int px, py; bool flag = true;
for (int j=; j<; ++j)
{
int tx = x + dx[i][j]; int ty = y + dy[i][j];
if (ok (tx, ty))
{
if (s[tx][ty] == '*')
{
cnt++; px = tx; py = ty;
}
}
else flag = false;
}
if (flag && cnt == )
{
s[px][py] = '.'; Q.push (make_pair (px, py));
}
}
}
} int main(void) //Codeforces Round #297 (Div. 2) D. Arthur and Walls
{
while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<n; ++i) scanf ("%s", s[i]);
BFS ();
for (int i=; i<n; ++i) printf ("%s\n", s[i]);
} return ;
} /*
5 5
.*.*.
*****
.*.*.
*****
.*.*.
6 7
***.*.*
..*.*.*
*.*.*.*
*.*.*.*
..*...*
*******
*/
BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls的更多相关文章
- 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 ...
- 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) 525D Arthur and Walls(dfs)
D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- 用NHibernate处理带属性的多对多关系
1.引言 老谭在面试开发者的时候,为了考察他们的数据库开发能力,经常祭出我的法宝,就是大学数据库教程中讲到的一个模式:学生选课.这个模式是这种: 在这个模式中,学生(Student)和课程(Cours ...
- 1987年国际C语言混乱代码大赛获奖的一行代码
macb() ? lpcbyu(&gbcq/_\021%ocq\012\0_=w(gbcq)/_dak._=}_ugb_[0q60)s+ 这是CoolShell博主之前做了一个非常有意思的在线 ...
- [ASP.NET MVC 小牛之路]05 - 使用 Ninject实现依赖注入
在[ASP.NET MVC 小牛之路]系列上一篇文章(依赖注入(DI)和Ninject)的末尾提到了在ASP.NET MVC中使用Ninject要做的两件事情,续这篇文章之后,本文将用一个实际的示例来 ...
- hadoop的一般端口使用
- LOJ#139. 树链剖分
LOJ#139. 树链剖分 题目描述 这是一道模板题. 给定一棵$n$个节点的树,初始时该树的根为 1 号节点,每个节点有一个给定的权值.下面依次进行 m 个操作,操作分为如下五种类型: 换根:将一个 ...
- os、sys和shutil模块
运行环境:python3 OS模块:os 模块提供了一个统一的操作系统的接口函数 下面的path指路径的意思 os.stat(file) #查询文件属性操作 os.sep #取代操作系统特定的路径分隔 ...
- fuse的write过程到底是怎么样的,128KB的buffer怎么用?
1. 在fuse/lib/fuse_kern_chan.c中有一个buffer设置 #define MIN_BUFSIZE 0x21000 //十进制132×1024 //为何不是128? 下面有一 ...
- UIFont 字体样式 [UIFont fontWithName~];
设置字体样式代码:[UIFont fontWithName:@"Arial-BoldMT" size:15] 下边一一列举: 一:Font Family: American Typ ...
- liunx命令之【查看某个端口号的使用情况】
第一:查看端口占用情况的命令:lsof -i:<端口号>
- Laravel配置nginx环境
前言: 之前坑的!一直在尝试配置,但都失败了,只能用着apache,但是最近想整合swoole到laravel,无奈当前测试服务器是nginx,我只能再尝试在nginx上搭laravel环境 方法如下 ...