博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
阅读量:6035 次
发布时间:2019-06-20

本文共 5111 字,大约阅读时间需要 17 分钟。

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
..... ..... ..... ..... 先转一发官方题解: http://codeforces.ru/blog/entry/17119?locale=en

To solve this problem we need to observe next fact. If in some square whith size 2 × 2 in given matrix there is exactly one asterisk, we must change it on dot. That is if in matrix from dots and asterisks is not square 2 × 2 in which exactly one asterisk and three dots, then all maximum size of the area from dots connected by sides represent rectangles.

Now solve the problem with help of bfs and this fact. Iterate on all asterisks in given matrix and if only this asterisk contains in some 2 × 2 square, change this asterisk on dot and put this position in queue. Than we need to write standart bfs, in which we will change asterisks on dots in all come out 2 × 2 squares with exactly one asterisk.

Asymptotic behavior of this solution — O(n * m), where n and m sizes of given matrix.

 

 

题意:

将 由 '.' 构成的联通区域变成矩形(通过将 '*' 改成 '.' )

题解:

很不错的一个想法,2*2 的矩形中,如果有一个 '*' 与三个 '.' ,那么这个 '*' 就一定要变成 ‘.' ,然后就bfs 找类似这种 2*2的矩形。

 

 

2015-03-28 05:12:33 GNU C++ Accepted 732 ms 27100 KB 

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 9 #define ll long long 10 int const N = 2005; 11 int const M = 205; 12 int const inf = 1000000000; 13 ll const mod = 1000000007; 14 15 using namespace std; 16 17 int n,m; 18 char s[N][N]; 19 int dirx[]={-1,-1,0,1,1,1,0,-1}; 20 int diry[]={ 0,1,1,1,0,-1,-1,-1}; 21 int used[N][N]; 22 23 typedef struct 24 { 25 int x; 26 int y; 27 }PP; 28 29 void ini() 30 { 31 int i; 32 for(i=1;i<=n;i++){ 33 scanf("%s",s[i]+1); 34 } 35 memset(used,0,sizeof(used)); 36 } 37 38 int isok(int x,int y) 39 { 40 if(x>=1 && x<=n && y>=1 && y<=m && s[x][y]=='.'){ 41 return 1; 42 } 43 else{ 44 return 0; 45 } 46 } 47 48 int check(int x,int y) 49 { 50 if(x<1 || x>n || y<1 || y>m || s[x][y]=='.') return 0; 51 int i; 52 int d[10]; 53 for(i=0;i<8;i++){ 54 d[i]=isok(x+dirx[i],y+diry[i]); 55 } 56 d[8]=d[0]; 57 // printf(" x=%d y=%d\n",x,y); 58 // for(i=0;i<=8;i++){ 59 // printf(" i=%d d=%d\n",i,d[i]); 60 // } 61 for(i=0;i<8;i+=2){ 62 //printf(" i=%d d=%d\n",i,d[i]); 63 if(d[i] && d[i+1] && d[i+2]) 64 return 1; 65 } 66 return 0; 67 } 68 69 void solve() 70 { 71 queue< PP > que; 72 PP te,nt; 73 int i,j; 74 for(i=1;i<=n;i++){ 75 for(j=1;j<=m;j++){ 76 if(check(i,j)==1){ 77 te.x=i;te.y=j; 78 //printf(" x=%d y=%d\n",te.x,te.y); 79 que.push(te); 80 used[i][j]=1; 81 } 82 } 83 } 84 85 while(que.size()>=1){ 86 te=que.front();que.pop(); 87 //printf(" x=%d y=%d\n",te.x,te.y); 88 s[te.x][te.y]='.'; 89 for(i=0;i<8;i++){ 90 nt.x=te.x+dirx[i]; 91 nt.y=te.y+diry[i]; 92 if(used[nt.x][nt.y]==0 && check(nt.x,nt.y)==1){ 93 used[nt.x][nt.y]=1; 94 que.push(nt); 95 } 96 } 97 } 98 } 99 100 void out()101 {102 int i;103 for(i=1;i<=n;i++){104 printf("%s\n",s[i]+1);105 }106 }107 108 int main()109 {110 //freopen("data.in","r",stdin);111 // freopen("data.out","w",stdout);112 //scanf("%d",&T);113 //for(int cnt=1;cnt<=T;cnt++)114 //while(T--)115 while(scanf("%d%d",&n,&m)!=EOF)116 {117 ini();118 solve();119 out();120 }121 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4373705.html

你可能感兴趣的文章
Linux Kernel系列一:开篇和Kernel启动概要
查看>>
BZOJ 2756: [SCOI2012]奇怪的游戏 网络流/二分
查看>>
master + worker模式的node多核解决框架——node-cluster
查看>>
Android如何实现超级棒的沉浸式体验
查看>>
使用node打造自己的命令行工具方法教程
查看>>
Express代理中间件问题与解决方案
查看>>
||和&&返回什么?
查看>>
linux在文件中查找指定字符串,然后根据查找结果来做进一步的处理
查看>>
在Oracle中删除所有强制性外键约束
查看>>
dhcp
查看>>
【R】R语言使用命令行参数 - [编程技巧(Program Skill)]
查看>>
经典算法题每日演练——第二题 五家共井
查看>>
存储过程中拼接的变量和点的问题
查看>>
ASP.NET那点不为人知的事(一)
查看>>
HTML 表格
查看>>
VMware 虚拟化编程(7) — VixDiskLib 虚拟磁盘库详解之三
查看>>
php 未实例化类调用方法的问题
查看>>
Anaconda jupyter notebook 出现 kernel error 解决办法
查看>>
我对读计算机软件专业硕士的几点看法
查看>>
用JS写CSS
查看>>