您的位置:首页 > 其它

Codeforces Round #375 (Div. 2) D. Lakes in Berland __ dfs+贪心+小根堆

2016-10-16 11:45 537 查看
D. Lakes in Berland

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1.
Each cell is either land or water. The map is surrounded by the ocean.

Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of
the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.

You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes
in Berland. Note that the initial number of lakes on the map is not less than k.

Input

The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) —
the sizes of the map and the number of lakes which should be left on the map.

The next n lines contain m characters
each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*'
(it means that the corresponding cell is land).

It is guaranteed that the map contain at least k lakes.

Output

In the first line print the minimum number of cells which should be transformed from water to land.

In the next n lines print m symbols
— the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.

It is guaranteed that the answer exists on the given data.

Examples

input
5 4 1
****
*..*
****
**.*
..**


output
1
****
*..*
****
****
..**


input
3 3 0
***
*.*
***


output
1
***
***
***


Note

In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3),
the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water
in the lower left corner is not a lake because this area share a border with the ocean.

Source

Codeforces Round #375 (Div. 2)

My Solution

dfs+贪心+小根堆

枚举所有未被标记过的 '.' 点, 先跑一遍dfs,如果是湖(没有到四周边界),则再跑一边来找出这个湖的大小,并记录在小根堆里 pq.push(ii(cnt, make_pair(i, j)));

然后对于pq里最小的前 pq.size() - k 个湖,跑一遍dfs把湖填上。//初始坐标为 (pq.top().second.first, pq.top().second.second)

然后MLE了,其实应该TLE吧,嘿嘿

dfs的时候没有处理本次dfs时访问过的点,所以可能跳不出递归即超内存也超时,所以每次dfs用 一个bool v[maxn][maxn] 数组标记,且每次使用前重置。

这样每次dfs每个点最多访问一次。

每个湖只找的时候访问2次,处理的时候访问一次,故每个点最多访问3次,

故 复杂度 O(n^2)

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int, pair<int, int> > ii;
const int maxn = 50 + 8;

string s[maxn];
bool vis[maxn][maxn], v[maxn][maxn];
priority_queue<ii, vector<ii>, greater<ii> > pq;
int n, m, k, cnt;

bool flag;
void dfs(int x, int y, int orix, int oriy)
{
if(v[x][y]) return;
if(s[x][y] == '*') return;
else{
if(x == 0 || x == n - 1 || y == 0 || y == m - 1) {flag = false; return;}
}
v[x][y] = true;
if(x + 1 < n && !(x + 1 == orix && y == oriy) ){
dfs(x + 1, y, x, y);
}
if(x - 1 >= 0 && !(x - 1 == orix && y == oriy) ){
dfs(x - 1, y, x, y);
}
if(y + 1 , m && !(x == orix && y + 1 == oriy) ){
dfs(x, y + 1, x, y);
}
if(y - 1 >= 0 && !(x == orix && y - 1 == oriy) ){
dfs(x, y - 1, x, y);
}
}

void _find(int x, int y, int orix, int oriy)
{
if(v[x][y]) return;
if(s[x][y] == '*') return;
else{
cnt++;
vis[x][y] = true;
}
v[x][y] = true;
if(x + 1 < n && !(x + 1 == orix && y == oriy) ){
_find(x + 1, y, x, y);
}
if(x - 1 >= 0 && !(x - 1 == orix && y == oriy) ){
_find(x - 1, y, x, y);
}
if(y + 1 , m && !(x == orix && y + 1 == oriy) ){
_find(x, y + 1, x, y);
}
if(y - 1 >= 0 && !(x == orix && y - 1 == oriy) ){
_find(x, y - 1, x, y);
}
}

void fillup(int x, int y, int orix, int oriy)
{
if(v[x][y]) return;
if(s[x][y] == '*') return;
else{
//cnt++;
//vis[x][y] = true;
;
}
v[x][y] = true;
if(x + 1 < n && !(x + 1 == orix && y == oriy) ){
fillup(x + 1, y, x, y);
}
if(x - 1 >= 0 && !(x - 1 == orix && y == oriy) ){
fillup(x - 1, y, x, y);
}
if(y + 1 , m && !(x == orix && y + 1 == oriy) ){
fillup(x, y + 1, x, y);
}
if(y - 1 >= 0 && !(x == orix && y - 1 == oriy) ){
fillup(x, y - 1, x, y);
}
//»ØËݵÄʱºò fillup
s[x][y] = '*';
}

int main()
{
#ifdef LOCAL
freopen("d.txt", "r", stdin);
//freopen("d.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

int ans = 0;
cin >> n >> m >> k;
for(int i = 0; i < n; i++){
cin >> s[i];
}
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
if(vis[i][j]) continue;
if(s[i][j] == '*') continue;
flag = true;
memset(v, false, sizeof v);
dfs(i, j, i, j);
if(flag){
cnt = 0;
memset(v, false, sizeof v);
_find(i, j, i, j);
if(cnt){
pq.push(ii(cnt, make_pair(i, j)));
}
}
}
}
int t = pq.size() - k;
while(t--){
ans += pq.top().first;
memset(v, false, sizeof v);
fillup(pq.top().second.first, pq.top().second.second, pq.top().second.first, pq.top().second.second);
pq.pop();
}

cout << ans << endl;
for(int i = 0; i < n; i++){
cout << s[i] << "\n";
}

#ifdef LOCAL
memset(vis, false, sizeof vis);
cout << endl;
}
#endif // LOCAL
return 0;
}

  Thank you!

                                                                                                           
                                   ------from ProLights 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: