Codeforces Round #297 (Div. 2) D - Arthur and Walls (深搜)
2016-08-18 11:38
615 查看
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.
Examples
input
output
input
output
input
output
以前见到过好几次这种类型的题,但是都没有思路,今天看了别
c7c3
人的代码,感觉很巧妙;找相邻的四个点,如果有一个点是×,就把它转换;
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.
Examples
input
5 5 .*.*. ***** .*.*. ***** .*.*.
output
.*.*. ***** .*.*. ***** .*.*.
input
6 7 ***.*.* ..*.*.* *.*.*.* *.*.*.* ..*...* *******
output
***...* ..*...* ..*...* ..*...* ..*...* *******
input
4 5 ..... ..... ..*** ..*..
output
..... ..... ..... .....
以前见到过好几次这种类型的题,但是都没有思路,今天看了别
c7c3
人的代码,感觉很巧妙;找相邻的四个点,如果有一个点是×,就把它转换;
#include<stdio.h> #include<string.h> char mp[2100][2100]; int n,m; void dfs(int a,int b) { int sum=0,i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) { if(mp[a+i][b+j]=='*') //找出相邻四个点中*的个数; sum++; } } if(sum==1) //如果等于1,就“吞掉” { for(i=0;i<2;i++) { for(j=0;j<2;j++) { mp[a+i][b+j]='.'; } } for(i=-1;i<2;i++) { for(j=-1;j<2;j++) //找周围8个方向继续深搜; { if(a+i>=0&&a+i<n-1&&b+j>=0&&b+j<m-1) dfs(a+i,b+j); } } } } int main() { int i,j,k,f; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { scanf("%s",&mp[i]); } for(i=0;i<n-1;i++) { for(j=0;j<m-1;j++) //不用到边,因为后面的深搜可以覆盖到; { dfs(i,j); } } for(i=0;i<n;i++) { printf("%s\n",mp[i]); } return 0; }
相关文章推荐
- 4.层次访问节点和创建节点
- Linux上配置DNS服务器
- python之pygame使surface对象移动起来(一)
- python学习 第七篇 sql优化
- linux 嵌入式自动检测u盘口并挂载
- Android判断横屏竖屏代码
- 1-1-1 裸机工具安装
- 动态计算UITableViewCell的高度
- GOTURN(Generic Object Tracking Using Regression Networks ECCV2016)
- PV EV AC BAC EAC ETC等计算公式
- sed基础用法
- 中国这10家慕课网站,您需要知道
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- 0-18 烧写开发板出产uboot
- js 获取站点应用名的简单实例
- 挣值管理(PV、EV、AC、SV、CV、SPI、CPI)记忆
- 安卓-bug记录
- MOV指令、LDR指令、LDR伪指令之间的区别
- BootStrap日期控件在模态框中选择时间下拉菜单无效的原因及解决办法(火狐下不能点击)
- Android Activity生命周期以及onSaveInstanceState、onRestoreInstanceState要点备忘