题目传送门

 /*
题意:问最少替换'*'为'.',使得'.'连通的都是矩形
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. Arcgis Engine(ae)接口详解(8):临时元素(element)

    //主地图的地图(map)对象 IMap map = null; IActiveView activeView = null; //IGraphicsContainer用于操作临时元素,可以通过map ...

  2. java验证码图片

    package com.su.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; impor ...

  3. 图像处理之基础---ffmpeg 中的图像缩放

    http://blog.csdn.net/bweaglegao/article/details/8540860 http://www.cnblogs.com/acloud/archive/2011/1 ...

  4. UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)

    题意:n个城市(n <= 10000), 有m条边(m <= 40000),每一个城市有一个维护费用Cost(i),除此之外,每条边的维修费用为去掉该边后不能通信的城市对数与边权的积.这个 ...

  5. java获取class的几种方式

    以获取Hello.class为例 public class Hello { public static void main(String[] args) { // TODO Auto-generate ...

  6. STM32的低功耗设置

    因为产品需求,系统功耗是一个很重要的考虑方面.好好看下STM32F103的低功耗问题,以便编写驱动. 1.STM32的电源 1.1 STM32电源框图 上面的电源中需要注意的是后备供电区域,这个部分由 ...

  7. eclipse输入提示 设置

  8. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. 数组、栈、堆(java基础知识五)

    1.数组概述.定义格式 * A:数组概念 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. 数组既可以存储基本数据类型,也可以存储引用数据类型. * B:数组定义格式 格式1:数据类型[ ...

  10. 【转载】Android Studio简单设置

    界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings --> Appearance --> Theme ,选择 Darcula 主题即可 ...