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]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- 一个简单的EBNF范式的实现
最近无事在看书的时候发现了这个东西刹那间突然觉得大学时候编译原理书上的的什么语法分析书.上下文无关等晦涩难懂的概念清晰了许多今天把它贴出来希望也能让你回想起些往事... 至于EBNF范式是什么东西,网 ...
- POJ 2750 Potted Flower(线段树+dp)
题目链接 虽然是看的别的人思路,但是做出来还是挺高兴的. 首先求环上最大字段和,而且不能是含有全部元素.本来我的想法是n个元素变为2*n个元素那样做的,这样并不好弄.实际可以求出最小值,总和-最小,就 ...
- Maven group, artifact or version defined in the pom file do not match the file ...
在把library上传到bintray空间的时候报以下错误 Could not upload to 'https://api.bintray.com/content/ping/maven/comm-a ...
- IMP-00009 And IMP-00028
导出文件异常结束” 错误,google一下,发现可能有如下原因导致 1.imp的数据太大,没有写buffer和commit 2.两个数据库字符集不同 3.从低版本exp的dmp文件,向高版本imp 4 ...
- 在C语言中使用libiconv进行编码转换的示例
libiconv_sample.c #include <stdio.h> #include <malloc.h> #include "libiconv/iconv.h ...
- 最短路Dijkstra算法的一些扩展问题
最短路Dijkstra算法的一些扩展问题 很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...
- p1697食物链
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说法 ...
- [存档]获取通讯录信息并写到SD卡上
下面是代码,不过没有字母表的顺序排列: package com.example.getcontacts; import java.io.FileWriter; import java.io.IOE ...
- Objective-C基础知识
内联函数 “内联函数”是一个很老的概念,在其他语言譬如C++语言中也出现了.所谓“内联函数”指的是“有函数的结构,但不具备函数的性质,类似于宏替换功能的代码块”. 在实际应用中,常常把规模较小.逻辑较 ...
- Struts2验证框架的配置及validation.xml常用的验证规则
转自:https://blog.csdn.net/wenwenxiong/article/details/55802655