题目传送门

 /*
题意:问最少替换'*'为'.',使得'.'连通的都是矩形
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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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  ...

  5. 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  ...

  6. 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 ...

  7. 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  ...

  8. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  9. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

随机推荐

  1. java中创建对象的五种方法

    用最简单的描述来区分new关键字和newInstance()方法的区别:newInstance: 弱类型.低效率.只能调用无参构造.new: 强类型.相对高效.能调用任何public构造. newIn ...

  2. linux内存操作--ioremap和mmap

    最近在做视频输出相关的东西,对于预留给framebuffer的内存使用不是很清楚,现在找到一些资料整理一下,以备使用.if (想看使用方法)  goto   使用方法: 对于一个系统来讲,会有很多的外 ...

  3. Intelij Idea 2016.3.4激活

    https://www.haxotron.com/jetbrains-intellij-idea-crack-123/ http://idea.lanyus.com/

  4. codeforces B. Sereja and Mirroring 解题报告

    题目链接:http://codeforces.com/contest/426/problem/B 题目意思:给出一个n * m的矩阵a,需要找出一个最小的矩阵b,它能通过several次的mirror ...

  5. 一步一步学Silverlight 2系列(30):使用Transform实现更炫的效果(下)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. Kafka使用经验小结

    本文尽量从一个使用者的角度去记录一些在实战当中使用Kfaka所需要关注的要点,这样可能会贴切更多的读者,本文并不会介绍太多的Kafka的一些架构层次设计的知识,因为网上已经有一大堆的重复搬运的资料任由 ...

  7. LA-2678 (尺取法)

    题意: 在一个长度为n的序列中,找到最短的长度序列,使其和大于等于s; 思路: two pointer ,水题; Ac代码: #include <bits/stdc++.h> /* #in ...

  8. 高性能框架gevent和gunicorn在web上的应用及性能测试

    WSGI Server有哪些: 比如 Flask,webpy,Django.CherryPy 都带着 WSGI server .当然性能都不好,自带的web server 更多的是测试用途, 发布时则 ...

  9. web_html-day1

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  10. 【旧文章搬运】Windows句柄表分配算法分析(一)

    原文发表于百度空间,2009-03-30========================================================================== 阅读提示: ...