codeforces 723D: Lakes in Berland
Description
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.
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.
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.
5 4 1
****
*..*
****
**.*
..**
1
****
*..*
****
****
..**
3 3 0
***
*.*
***
1
***
***
***
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.
正解:BFS
解题报告:
根据题目意思,先把海洋部分找出来,标记一下(BFS即可)。然后对于所有的处在内部的湖,BFS求出每个湖的面积,并且按湖的面积排序,优先把面积小的湖填掉。
结果一直RE+WA的原因居然是BFS的时候,我的标记经过的数组是一个萎的,我是在从队首取出时才标记的,事实上入队的时候就应该考虑!注意细节!
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
const int MAXL = ;
const int MOD = ;
int n,m,k,cnt,ans;
int a[MAXN][MAXN];
bool vis[MAXN][MAXN];
int head,tail,dui[MAXL][];
int yi[][]={{,},{-,},{,},{,-}};
struct lake{
int val;
int x,y;
}hu[MAXN*MAXN];
inline bool cmp(lake q,lake qq){ return q.val<qq.val; }
inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void get_ocean(int inx,int iny){
head=tail=; dui[++tail][]=inx; dui[tail][]=iny; vis[inx][iny]=;
int ux,uy,nowx,nowy;
while(head!=tail) {
head++; head%=MOD; ux=dui[head][]; uy=dui[head][];
for(int i=;i<;i++) {
nowx=ux+yi[i][]; nowy=uy+yi[i][]; if(nowx<= || nowy<= || nowx>n || nowy>m) continue;
if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
tail++; tail%=MOD; dui[tail][]=nowx; dui[tail][]=nowy; vis[nowx][nowy]=;
}
}
} inline int BFS(int inx,int iny){
head=tail=; dui[++tail][]=inx; dui[tail][]=iny; vis[inx][iny]=;
int ux,uy,nowx,nowy,total=;
while(head!=tail) {
head++; head%=MOD; ux=dui[head][]; uy=dui[head][];
total++;
for(int i=;i<;i++) {
nowx=ux+yi[i][]; nowy=uy+yi[i][]; if(nowx<= || nowy<= || nowx>n || nowy>m) continue;
if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
tail++; tail%=MOD; dui[tail][]=nowx; dui[tail][]=nowy; vis[nowx][nowy]=;
}
}
return total;
} inline void change(int inx,int iny){
head=tail=; dui[++tail][]=inx; dui[tail][]=iny; vis[inx][iny]=;
int ux,uy,nowx,nowy;
while(head!=tail) {
head++; head%=MOD; ux=dui[head][]; uy=dui[head][]; a[ux][uy]=;
for(int i=;i<;i++) {
nowx=ux+yi[i][]; nowy=uy+yi[i][]; if(nowx<= || nowy<= || nowx>n || nowy>m) continue;
if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
tail++; tail%=MOD; dui[tail][]=nowx; dui[tail][]=nowy; vis[nowx][nowy]=;
}
}
} inline void work(){
n=getint(); m=getint(); k=getint(); char c;
for(int i=;i<=n;i++) for(int j=;j<=m;j++) { c=getchar(); while(c!='*' && c!='.') c=getchar(); if(c=='.') a[i][j]=; }
for(int i=;i<=n;i++) { if(!vis[i][] && a[i][]) get_ocean(i,); if(!vis[i][m] && a[i][m]) get_ocean(i,m); }
for(int i=;i<m;i++) { if(!vis[][i] && a[][i]) get_ocean(,i); if(!vis[n][i] && a[n][i]) get_ocean(n,i); }
for(int i=;i<n;i++) for(int j=;j<m;j++) if(!vis[i][j] && a[i][j]) { hu[++cnt].val=BFS(i,j); hu[cnt].x=i; hu[cnt].y=j; }
memset(vis,,sizeof(vis)); sort(hu+,hu+cnt+,cmp);
for(int i=;i<=cnt-k;i++) { ans+=hu[i].val; change(hu[i].x,hu[i].y); }
printf("%d\n",ans);
for(int i=;i<=n;i++) { for(int j=;j<=m;j++) if(a[i][j]) printf("."); else printf("*"); printf("\n"); }
} int main()
{
work();
return ;
}
codeforces 723D: Lakes in Berland的更多相关文章
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 723D】Lakes in Berland (dfs)
海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...
- codeforce375div2-D. Lakes in Berland 搜索
Lakes in Berland 题意与解释:这道题就是求图中被围起来的点群,问最少去掉几个点,可以使得孤立的点群数目为K; 因为自己写的代码又长又had bugs. 我自己写的bfs,想着是先染色, ...
随机推荐
- 经典算法和OJ网站(开发者必备-转)
一. Online Judge简介: Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序多种程序(如C.C++.Pascal)源代码,系统对源代码进行编译和执行,并通过 ...
- 拒绝了对对象 'XXX' (数据库 'XXX',架构 'dbo')的 SELECT 权限
2010-04-17 23:16 在IIS里测试ASP.NET网站时会遇到这样的问题(ASP.NET+SQL2005)我自己的解决方法是这样的: 1.打开SQL2005管理界面(没有安装SQLServ ...
- 使Eclipse符合Java编程规范
编程规范是很重要的东西,能让团队的代码易于阅读和维护,也便于日后的功能扩展. 工欲善其事必先利其器!作为一个Java程序员,与Eclipse打交道可能是一辈子的事情.将Eclipse设置为符合公司编程 ...
- mac OS X Yosemite 上编译hadoop 2.6.0/2.7.0及TEZ 0.5.2/0.7.0 注意事项
1.jdk 1.7问题 hadoop 2.7.0必须要求jdk 1.7.0,而oracle官网已经声明,jdk 1.7 以后不准备再提供更新了,所以趁现在还能下载,赶紧去down一个mac版吧 htt ...
- Matlab滤波器设计(转)
滤波器设计是一个创建满足指定滤波要求的滤波器参数的过程.滤波器的实现包括滤波器结构的选择和滤波器参数的计算.只有完成了滤波器的设计和实现,才能最终完成数据的滤波. 滤波器设计的目标是实现数据序列的频率 ...
- Python2.7-异常和工具
来自<python学习手册第四版>第七部分,而且本书发布的时候3.1还未发布,所以针对本书的一些知识会有些滞后于python的版本,具体更多细节可以参考python的标准手册. 一.异常基 ...
- HTML5 — 让拖放变的流行起来
先上 Demo,尽量用 chrome,代码可参考 Github. 在 HTML5 出现之前,页面元素的拖放需要监听 mousedown.mouseover 以及 mouseup 等一系列事件,然后改变 ...
- JavaScript中的类型转换(二)
说明: 本篇主要讨论JavaScript中各运算符对运算数进行的类型转换的影响,本文中所提到的对象类型仅指JavaScript预定义的类型和程序员自己实现的对象,不包括宿主环境定义的特殊对象(比如浏览 ...
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...
- node 学习笔记 - path 处理
本文同步自我的个人博客:http://www.52cik.com/2015/12/04/learn-node-path.html path 模块是 node 用于整理.转换.合并路径的神器,只要是路径 ...