[原创]Codeforces Round #297 (Div. 2) D - Arthur and Walls [思维]【思维】

2016-11-03 13:13:07 Tabris_ 阅读数:193


博客爬取于2020-06-14 22:42:53
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/53020524


题目链接:http://codeforces.com/contest/525/problem/D
--------------------------------------.
D. Arthur and Walls
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
input
5 5
.*.*.
*****
.*.*.
*****
.*.*.
output
.*.*.
*****
.*.*.
*****
.*.*.

input
6 7
***.*.*
..*.*.*
*.*.*.*
*.*.*.*
..*...*
*******
output
***...*
..*...*
..*...*
..*...*
..*...*
*******

input
4 5
.....
.....
..***
..*..
output
.....
.....
.....
.....

--------------------------------------.

题目大意 :
就是给你一个nm的矩阵 里面‘’代表着墙 ‘.’代表空地
现在让你尽可能少的移开墙 然后使每一片联通的空地都成为一个矩形

解题思路:
最开始看到这题的想法就是搜索 判断联通快然后让这一部分所在的最小矩形全部变成‘.’ 然后发现不行 有可能 操作之后的两个矩形又连到一起了,这样的话所有操作都需要再从新来一下,图还特别大 所以一定会超时

然后左想右想 还是不明白
最后看了 别人的代码 明白了
只需要看每一个2*2的部分就行了
对于连不连通 也非常好判断了

而判断用不用移走墙 就更好想了 对于每一个22内 只有一个‘’号的时候一定要移走 其他情况就不用
想明白这句话 这道题就可以解决了

最后处理的时候用一个队列 维护一下坐标即可
每个点最多也只是遍历一遍,这样就不会超时了 而且操作也更简单
详情看代码就好

附本题代码
--------------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# include<bits/stdc++.h>

# define abs(x) ((x)>0?(x):-(x))
using namespace std;
typedef long long LL;
const int maxn = 505;

char mp[2020][2020];
bool vis[2020][2020];
queue<pair<int ,int > >q;

int n,m;

bool check(int x,int y){//检查2*2内是不是只有一个 '*'
return x>=0&&y>=0&&x<n-1&&y<m-1&&
((mp[x][y]=='*')+(mp[x+1][y]=='*')+(mp[x][y+1]=='*')+(mp[x+1][y+1]=='*')==1);
}

int main(){

while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++) scanf("%s",mp[i]);
for(int i=0;i<n;i++) for(int j=0;j<m;j++) vis[i][j]=0;

for(int i=0;i<n-1;i++){
for(int j=0;j<m-1;j++){//对于2*2 枚举的是左上的坐标 其实哪里都一样
if(check(i,j)) q.push(make_pair(i,j)),vis[i][j]=1;
}
}

int x,y;
while(!q.empty()){
x=q.front().first,y=q.front().second; q.pop();
for(int i=0;i<2;i++) for(int j=0;j<2;j++) mp[x+i][y+j]='.';//移走墙

for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++){
if(!vis[x+i][y+j]&&check(x+i,y+j)){//移走墙之后 要在判断一下周围的有没有需要移走墙的位置
q.push(make_pair(x+i,y+j)),vis[x+i][y+j]=1;
}
}

}
for(int i=0;i<n;i++) puts(mp[i]);
}
return 0;
}

--------------------------------.