Codeforces Round #297 (Div. 2)D. Arthur and Walls

Time Limit: 2 Sec  Memory Limit: 512 MB
Submit: xxx  Solved: 2xx

题目连接

http://codeforces.com/contest/525/problem/D

Description

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 Input

Input
  1. 5 5
    .*.*.
    *****
    .*.*.
    *****
    .*.*.
 
Input
  1. 6 7
    ***.*.*
    ..*.*.*
    *.*.*.*
    *.*.*.*
    ..*...*
    *******
 
Input
  1. 4 5
    .....
    .....
    ..***
    ..*..
 

Sample Output

Output
  1. .*.*.
    *****
    .*.*.
    *****
    .*.*.
Output
  1. ***...*
    ..*...*
    ..*...*
    ..*...*
    ..*...*
    *******
Output
  1. .....
    .....
    .....
    .....

HINT

题意:

给你一个n*m的田地,有一些*的地方是可以移除变成"。"的,然后问你移除最少的"*",使的每一个"。"的联通块都是矩形

题解:

爆搜! DFS、BFS、乱搞都能过,只要姿势优美!

我说一种解法,对于每一个"*",扫(i+1,j)(i,j+1)(i+1,j+1)这3个位置,假如都是"。"的话,那么"*"必然也应该变成"。",然后我们再回溯一下就好啦 ,这是一个O(n^2)的算法,不过这道题会卡常数
~\(≧▽≦)/~啦啦啦,讲完啦~

代码:

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. typedef long long ll;
  16. using namespace std;
  17. //freopen("D.in","r",stdin);
  18. //freopen("D.out","w",stdout);
  19. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  20. #define maxn 4001
  21. #define mod 10007
  22. #define eps 1e-9
  23. //const int inf=0x7fffffff; //无限大
  24. const int inf=0x3f3f3f3f;
  25. /*
  26.  
  27. */
  28. //**************************************************************************************
  29. inline ll read()
  30. {
  31. int x=,f=;char ch=getchar();
  32. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  33. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  34. return x*f;
  35. }
  36. string g[maxn];
  37. int n,m;
  38. void check(int x,int y)
  39. {
  40. if(x<||x>=n||y<||y>=m)
  41. return;
  42. int cnt=;
  43. for(int i=;i<;i++)
  44. for(int j=;j<;j++)
  45. if(g[x+i][y+j]=='.')
  46. cnt++;
  47. if(cnt==)
  48. {
  49. for(int i=;i<;i++)
  50. for(int j=;j<;j++)
  51. g[x+i][y+j]='.';
  52. for(int i=-;i<;i++)
  53. for(int j=-;j<;j++)
  54. check(x+i,y+j);
  55. }
  56. return;
  57. }
  58. int main()
  59. {
  60. n=read(),m=read();
  61. for(int i=;i<n;i++)
  62. {
  63. cin>>g[i];
  64. }
  65. for(int i=;i<n;i++)
  66. {
  67. for(int j=;j<m;j++)
  68. {
  69. check(i,j);
  70. }
  71. }
  72. for(int i=;i<n;i++)
  73. {
  74. cout<<g[i]<<endl;
  75. }
  76. }

Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索的更多相关文章

  1. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

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

  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. 80.YCrCb - YUV - RGB之间的介绍

    一,引言 YUV(亦称YCrCb)是被欧洲电视系统所采用的一种颜色编码方法(属于PAL).YUV主要用于优化彩色视频信号的传输,使其向后兼容老式黑白电视.与RGB视频信号传输相比,它最大的优点在于只需 ...

  2. HDU 6214 Smallest Minimum Cut 最小割,权值编码

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...

  3. python 之datetime库学习

    # -*- coding:utf-8 -*- import refrom datetime import datetime, timezone, timedelta def rec_time():   ...

  4. 一文看懂python主要应用领域或应用场景

    Python简介 Python(英国发音:/ˈpaɪθən/美国发音:/ˈpaɪθɑːn/),是一种面向对象的解释型计算机程序设计语言,由荷兰人GuidovanRossum于1989年发明,第一个公开 ...

  5. RESTful API 和 Django REST framework

    100天 cmdb最后一天 #RESTful API - 定义规范 如get就是请求题 - 面向资源编程 把网络任何东西都当作资源 #给一个url,根据方法的不同对资源做不同的操作 #返回结果和状态码 ...

  6. python 图片上传写入磁盘功能

    本文是采取django框架,前端上传图片后端接收后写入磁盘,数据库记录图片在磁盘上的路径(相对),以下是前端上传到后端入库的基本流程 一. html代码 <!DOCTYPE html> & ...

  7. Linux学习笔记:pwd与dirs的区别

    在Linux中可以使用pwd和dirs进行当前目录查看,分别用于显示当前目录和显示完整目录记录.具体如下: 1.pwd 显示当前目录 2.dirs 显示目录堆叠中的记录 END 2018-08-21  ...

  8. 20165203 预备作业3 Linux安装及学习

    一.安装虚拟机 1.下载问题:当自己在虚拟机上下载ubuntu时,总是下载好长时间,而且最后下载失败,这让我很苦恼. 解决方案:求助同学后,同学给了我一个中文版官网的网址http://cn.ubunt ...

  9. Visual Studio 2017 百度云下载

    链接: https://pan.baidu.com/s/1kFjGwyj5HwabvmJKiyLF_g 提取码: 关注公众号[GitHubCN]回复获取   秘钥Enterprise:NJVYC-BM ...

  10. PHP session 写入数据库中的方法

    首先解释下为什么要把session 写到数据库中呢,session 一般默认是以文件的形式放在php.ini 配置的目录中的, 如果你的网站实现了多台服务器负载均衡,这样用户访问你的网站,可能进入的服 ...