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. Unity3d-Socket之龙一编年史network.dll分析(1)

    今天闲着无聊,看到群里的老大共享了反编译的工具,就下载下来玩下. 说道反编译我个人不太推崇反编译,感觉不道德,毕竟是人家的代码,但是又回想一下,我们拿代码看是抱着学习的态度又有何不可,所谓既是民族的也 ...

  2. prometheus,alertmanager 报警配置详解

    vim prometheus.yml global: scrape_interval: 15s external_labels: monitor: 'codelab-monitor' scrape_c ...

  3. cpu个数、核数、线程数、Java多线程关系的理解

    cpu个数.核数.线程数.Java多线程关系的理解 2017年12月08日 15:35:37 一 cpu个数.核数.线程数的关系 cpu个数:是指物理上,也及硬件上的核心数: 核数:是逻辑上的,简单理 ...

  4. iOS:quartz2D绘图小项目(涂鸦画板)

    介绍:学了quartz2D的绘图知识后,我根据它的一些功能制作了一个小项目:涂鸦画板. 功能:绘制各种图形,还可以选取相册上的照片做涂鸦,然后保存到相册中.其中,还包括功能有:颜色的选取.线宽的选取. ...

  5. LeetCode56:Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. 【转】php中的会话机制(2)

    原文:https://segmentfault.com/a/1190000000468220 发现,在调用session_start()的时候, session_start() 里面应该是有调用类似 ...

  7. 【DataStrcutre】Introduction and description of Binary Trees

    [Definitions] Here is the recursive definition of a binary tree: A binary tree is either the empty s ...

  8. DevExpress TreeList使用

    using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...

  9. [Tools] Unlock TypeScript's Features in Vanilla JS with @ts-check and JSDoc

    TypeScript can help you with your plain JavaScript files if you add a simple //@ts-check comment. Th ...

  10. JVM性能监控工具(一)-jdk命令行工具

    转载:http://blog.csdn.net/top_code/article/details/51456186 当系统出bug需要定位问题的时候,知识.经验是关键基础,数据是依据,工具是运用知识处 ...