[原创]Codeforces Round #297 (Div. 2) D - Arthur and Walls [思维]【思维】
[原创]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 | input |
--------------------------------------.
题目大意 :
就是给你一个nm的矩阵 里面‘’代表着墙 ‘.’代表空地
现在让你尽可能少的移开墙 然后使每一片联通的空地都成为一个矩形
解题思路:
最开始看到这题的想法就是搜索 判断联通快然后让这一部分所在的最小矩形全部变成‘.’ 然后发现不行 有可能 操作之后的两个矩形又连到一起了,这样的话所有操作都需要再从新来一下,图还特别大 所以一定会超时
然后左想右想 还是不明白
最后看了 别人的代码 明白了
只需要看每一个2*2的部分就行了
对于连不连通 也非常好判断了
而判断用不用移走墙 就更好想了 对于每一个22内 只有一个‘’号的时候一定要移走 其他情况就不用
想明白这句话 这道题就可以解决了
最后处理的时候用一个队列 维护一下坐标即可
每个点最多也只是遍历一遍,这样就不会超时了 而且操作也更简单
详情看代码就好
附本题代码
--------------------------------.
1 | # include<bits/stdc++.h> |
--------------------------------.