-
Notifications
You must be signed in to change notification settings - Fork 10
/
Codeforces: 723D - Lakes in Berland.cpp
122 lines (103 loc) · 2.08 KB
/
Codeforces: 723D - Lakes in Berland.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// Codeforces: 723D - Lakes in Berland.
/// Category: greedy + dfs.
#include<bits/stdc++.h>
using namespace std;
struct str
{
int x,y;
int sz;
};
const int mx=52;
bool vis[mx][mx];
char mat[mx][mx];
bool border_area;
int conts=0;
void dfs(int x,int y,int n,int m)
{
if(x<1 || x>n || y<1 || y>m) return;
if(vis[x][y]) return;
if(y==1||y==m||x==n||x==1) {border_area=true;return;}
/// adjacent
conts++;
vis[x][y]=true;
if(mat[x][y+1]=='.') dfs(x,y+1,n,m);
if(mat[x][y-1]=='.') dfs(x,y-1,n,m);
if(mat[x+1][y]=='.') dfs(x+1,y,n,m);
if(mat[x-1][y]=='.') dfs(x-1,y,n,m);
}
void fill_up(int x,int y,int n,int m)
{
if(x<1 || x>n || y<1 || y>m) return;
if(vis[x][y]) return;
/// adjacent
vis[x][y]=true;
mat[x][y]='*';
if(mat[x][y+1]=='.') fill_up(x,y+1,n,m);
if(mat[x][y-1]=='.') fill_up(x,y-1,n,m);
if(mat[x+1][y]=='.') fill_up(x+1,y,n,m);
if(mat[x-1][y]=='.') fill_up(x-1,y,n,m);
}
bool comp(str a,str b)
{
return (a.sz<b.sz);
}
int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>mat[i][j];
}
}
vector< str >v;
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(!vis[i][j] && mat[i][j]=='.')
{
border_area=false;
conts=0;
dfs(i,j,n,m);
if(!border_area)
{
str st;
st.x=i;
st.y=j;
st.sz=conts;
v.push_back(st);
}
}
}
}
cout<<endl;
memset(vis,false,sizeof(vis));
sort(v.begin(),v.end(),comp);
int siz=(int)v.size();
siz-=k;
int i=0;
int x,y;
int cells=0;
while(siz--)
{
x=v[i].x;
y=v[i].y;
cells+=v[i].sz;
fill_up(x,y,n,m);
i++;
}
cout<<cells<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<mat[i][j];
}
cout<<endl;
}
return 0;
}