Codeforeces - 723D -Lakes in Berland
2017-01-22 11:44
351 查看
Codeforeces - 723D -Lakes in Berland
time limit per test2 secondsmemory limit per test256 megabytes
inputstandard input
outputstandard 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
0****
0*..*
0****
0**.*
0..**
output
1
0****
0*..*
0****
0****
0..**
input
3 3 0
0***
0*.*
0***
output
1
0***
0***
0***
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.
PS. 由于这个编辑器的原因,不能直接把星号 * 打出来,所以我加了一个前导零,输出的时候请无视前导零。
题目大意:
有一个 n*m 的矩阵,有湖、海、陆地三种属性,靠边的 . 是海,内陆的 . 是湖。求要填的土块的数量。
分析:
只要把海和湖区分开来,逐一填土就行了。
AC代码:
#include <iostream> # 4000 include <cstring> #include <algorithm> using namespace std; int n,m,k; char s[55][55]; bool vis[55][55]; int dx[6]={0,0,1,-1}; int dy[6]={1,-1,0,0}; int num,cnt,islake; int ans; struct lake{/*此处用到结构体,用于记录湖的任意一点的坐标,及整个湖的面积。*/ int x,y,num; }lk[3600]; bool cmp(lake a,lake b) { return a.num<b.num; } void dfs(int x,int y) { vis[x][y]=1; num++; if (x==0||x==n-1||y==0||y==m-1) islake=0;//海 for (int i=0;i<4;i++) { int nx=x+dx[i],ny=y+dy[i]; if (nx>=0&&nx<n&&ny>=0&&ny<m &&s[nx][ny]=='.' &&!vis[nx][ny]) dfs(nx,ny); } } void fill_in(int x,int y) { vis[x][y]=1; ans++; s[x][y]='*'; for (int i=0;i<4;i++) { int nx=x+dx[i],ny=y+dy[i]; if (nx>=0&&nx<n&&ny>=0&&ny<m &&s[nx][ny]=='.' &&!vis[nx][ny]) fill_in(nx,ny); } } int main() { cin>>n>>m>>k; for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>>s[i][j]; for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (!vis[i][j] && s[i][j]=='.') { num=0; islake=1; dfs(i,j); if (islake) lk[cnt++] = lake{i,j,num};//把湖的坐标及面积存入 } memset (vis,0,sizeof (vis)); sort (lk,lk+cnt,cmp); for (int l=0;l<cnt-k;l++) { for (int i=0;i<n;i++) { for (int j=0;j<m;j++) { if (i==lk[l].x&&j==lk[l].y) fill_in(i,j); } } } cout<<ans<<endl; for (int i=0;i<n;i++){ for (int j=0;j<m;j++) cout<<s[i][j]; cout<<endl; } } //这题做得我头都晕了,写了大概四五份不同的代码,其实思路都差不多,可是不懂为什么,就是有地方卡壳。而这一份代码还是我从博客园一个大神那拿来的,感谢 @flipped 。
未AC代码:
#include <iostream> #include <algorithm> #include <cstring> using namespace std; char s[55][55]; int vis[55][55],seq[55][3]; int num=0;int n,m,k; struct node { int x,y,cnt; }; void bfs1(int x,int y) { int head=1,tail=1; vis[x][y]=-1; seq[head][1]=x;seq[head][2]=y; while (head<=tail) { int xv=seq[head][1],yv=seq[head][2]; for (int i=-1;i<=1;i++) { for (int j=-1;j<=1;j++) { if (i*j==0 && s[xv+i][yv+j]=='.' && !vis[xv+i][yv+j] && xv+i>0&&xv+i<=n&&yv+j>0&&yv+j<=m) { tail++; seq[tail][1]=xv+i;seq[tail][2]=yv+j; vis[xv+i][yv+j]=-1; } } } head++; } } node bfs2(int x,int y) { int cntt=1; vis[x][y]=1; int head=1,tail=1; seq[head][1]=x;seq[head][2]=y; while (head<=tail) { int xv=seq[head][1],yv=seq[head][2]; for (int i=-1;i<=1;i++) { for (int j=-1;j<=1;j++) { if (i*j==0 && !vis[xv+i][yv+j] && s[xv+i][yv+j]=='.' && xv+i>0&&xv+i<=n&&yv+j>0&&yv+j<=m) { tail++; seq[tail][1]=xv+i;seq[tail][2]=yv+j; vis[xv+i][yv+j]=1; } } } head++;cntt++; } node tmp; tmp.x=seq[tail][1]; tmp.y=seq[tail][2]; tmp.cnt=cntt; return tmp; } void fill_in(int x,int y) { s[x][y]='*';num++; for (int i=-1;i<=1;i++) { for (int j=-1;j<=1;j++) { if (i*j==0 && s[x+i][y+j]=='.' && vis[x+i][y+j]==1 &&x+i>0&&y+j>0&&x+i<=n&&y+j<=m) {s[x+i][y+j]='*';fill_in(x+i,y+j);} } } } int cmp(node a,node b) { return a.cnt<b.cnt; } int main() { int k1=0; node now[2550]; cin>>n>>m>>k; for (int i=1;i<=n;i++)//cin { for (int j=1;j<=m;j++) { cin>>s[i][j]; } } for (int i=1;i<=n;i++)//海 { for (int j=1;j<=m;j++) { if ((i==1||i==n||j==1||j==m) && s[i][j]=='.' && !vis[i][j]) bfs1(i,j); } } memset(seq,0,sizeof (seq)); for (int i=1;i<=n;i++)//湖 { for (int j=1;j<=m;j++) { if (s[i][j]=='.' && !vis[i][j]) now[++k1]=bfs2(i,j); } } sort (now+1,now+1+k1,cmp); for (int i=1;i<=k1-k;i++) fill_in(now[i].x,now[i].y); cout<<num<<endl; for (int i=1;i<=n;i++)//cout { for (int j=1;j<=m;j++) { cout<<s[i][j]; } cout<<endl; } } /*这份其实也是差不多的思路,只不过用的是 bfs 来做的,写起来略麻烦,最后还是 WA 了,我实在是看不出有什么地方有漏洞,希望有大神看到可以指点!谢谢!*/
相关文章推荐
- codeforces723D-Lakes in Berland-DFS
- Codeforces 723D - Lakes in Berland
- codeforces723D Lakes in Berland 搜索
- codeforces 723D: Lakes in Berland
- codeforces#375(div.2)723D - Lakes in Berland dfs+bfs
- 【codeforces 723D】【DFS (Floodfill)】D. Lakes in Berland【把多于k的连通块填起来,最外面的连通块直接忽视】
- CodeForces 723D-Lakes in Berland(BFS DFS)
- 【Codeforces 723D】Lakes in Berland (dfs)
- 【29.70%】【codeforces 723D】Lakes in Berland
- CodeForces - 723D - D. Lakes in Berland
- D.Lakes in Berland(DFS)
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- F - Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
- D. Lakes in Berland(dfs好题)
- F - Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland __ dfs+贪心+小根堆
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
- D. Lakes in Berland