D. Arthur and Walls
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of the city with a nice price.

Plan of the apartment found by Arthur looks like a rectangle n × m consisting of squares of size 1 × 1.
Each of those squares contains either a wall (such square is denoted by a symbol "*" on the plan) or a free space (such square is denoted on the plan by a
symbol ".").

Room in an apartment is a maximal connected area consisting of free squares. Squares are considered adjacent if they share a common side.

The old Arthur dream is to live in an apartment where all rooms are rectangles. He asks you to calculate minimum number of walls you need to remove in order to achieve this goal. After removing a wall from a square it becomes a free square. While removing the
walls it is possible that some rooms unite into a single one.

Input

The first line of the input contains two integers n, m (1 ≤ n, m ≤ 2000)
denoting the size of the Arthur apartments.

Following n lines each contain m symbols
— the plan of the apartment.

If the cell is denoted by a symbol "*" then it contains a wall.

If the cell is denoted by a symbol "." then it this cell is free from walls and also this cell is contained in some of the rooms.

Output

Output n rows each consisting of m symbols
that show how the Arthur apartment plan should look like after deleting the minimum number of walls in order to make each room (maximum connected area free from walls) be a rectangle.

If there are several possible answers, output any of them.

Sample test(s)
input
5 5
.*.*.
*****
.*.*.
*****
.*.*.
output
.*.*.
*****
.*.*.
*****
.*.*.
input
6 7
***.*.*
..*.*.*
*.*.*.*
*.*.*.*
..*...*
*******
output
***...*
..*...*
..*...*
..*...*
..*...*
*******
input
4 5
.....
.....
..***
..*..
output
.....
.....
.....
.....

题目链接:点击打开链接

给出n * m的地图, '*'代表墙, '.'代表房间, 墙能够变为房间, 现要求房间为长方形, 问最少须要变化后的房间.

dfs的条件是四个点组成的形状仅仅有一个'*', 于是将这个墙变为房间, 继续dfs, 能够使得变化最少.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 2005;
int n, m;
char maze[MAXN][MAXN];
void Dfs(int x, int y)
{
if(x >= n - 1 || y >= m - 1 || x < 0 || y < 0) return;
int xx, yy, s = 0;
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
if(maze[x + i][y + j] == '*') {
s++;
xx = x + i;
yy = y + j;
}
if(s == 1) {
maze[xx][yy] = '.';
for(int i = -1; i < 1; ++i)
for(int j = -1; j < 1; ++j)
Dfs(xx + i, yy + j);
} }
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", maze[i]);
for(int i = 0; i < n - 1; ++i)
for(int j = 0; j < m - 1; ++j)
Dfs(i, j);
for(int i = 0; i < n; ++i)
printf("%s\n", maze[i]);
return 0;
}

Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)的更多相关文章

  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. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

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

  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. 基于Python的交互式访问

    应用迁移中遇到一些有特殊要求的应用,比如需要通过交互生成一些新的config文件,然后启动应用需要依赖于这些文件,这样在构建镜像的时候基本上是没有办法把这些文件固定的,因为他需要根据运行环境去进行动态 ...

  2. saltstack之crontab管理用法

    一.创建定时任务 crontab: #脚本脚识 cron.present: #模板:cron 计划任务 功能:present - name: /usr/sbin/ntpdate times.aliyu ...

  3. jQuery对象的序列化详解

    一.param() 方法创建数组或对象的序列化表示. 该序列化值可在进行 AJAX 请求时在 URL 查询字符串中使用. 语法: jQuery.param(object,traditional) ob ...

  4. maven工程小红叉处理方法

    搞了个Maven工程在Eclipse上,刚开始说JDK版本不对,编译的时候老报错误,很容易搞明白, 本地JDK版本为1.7.0_79: diamond operator is not supporte ...

  5. CentOS6.8 4.4.43内核 安装PF_RING

    环境: 系统:CentOS 6.8 内核版本:4.4.43 PF_RING版本:6.9.0 编译PF_RING需要内核源码,由于我的机器上只有4.4.43版本的modules和4.4.43的源码,并没 ...

  6. JMS与Spring之一(用JmsTemplate同步收发消息) --转

    转自:http://blog.csdn.net/moonsheep_liu/article/details/6683190

  7. Storm---DirectGroup(直接分组)

    以单词分割计数为例实现Storm的DirectGroup分组: 1.Spout实现 Spout是Storm数据源头,使用DirectGroup方式将Spout数据发送指定的Bolt,需注意: 1).S ...

  8. iOS NSFileManager对沙盒文件及目录添加删除操作

    iOS 使用 NSFileManager对沙盒里面的文件和目录,增加,修改,删除操作: - (void)viewDidLoad { [super viewDidLoad]; self.title = ...

  9. Android 内存泄漏分析利器——leakcanary

    LeakCanary Android 和 Java 内存泄露检测. “A small leak will sink a great ship.” - Benjamin Franklin 千里之堤, 毁 ...

  10. 算法笔记_050:硬币收集问题(Java)

    目录 1 问题描述 2 解决方案 2.1 动态规划法   1 问题描述 在n*m格木板中放有一些硬币,每格的硬币数目最多为一个,在木板左上方的一个机器人需要收集尽可能多的硬币并把它们带到右下方的单元格 ...