D. Lakes in Berland (DFS或者BFS +连通块
https://blog.csdn.net/guhaiteng/article/details/52730373 参考题解
http://codeforces.com/contest/723/problem/D 原题目
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=2e5+; int n,m,k;
char mp[][];
int vis[][]={},tian[]={};
int dx[]={,-,,},dy[]={,,,-};
int nn=,cnt=,flag=,num=; pair<int,int>v[]; bool cmp(pair<int,int> c ,pair<int,int> d)
{
return c.second<d.second;
} void dfs(int nx,int ny)
{
num++; //块数++
vis[nx][ny]=cnt; //是属于第几个块的 就涂第几种颜色
if(nx==n||nx==||ny==||ny==m) flag=; //如果是海,就不能记入v中
for(int i=;i<;i++)
{
int x=nx+dx[i],y=ny+dy[i];
if(x>n||x<||y<||y>m||mp[x][y]!='.'||vis[x][y]) continue;
dfs(x,y);
}
} int main()
{
cin>>n>>m>>k;
for(int i=;i<=n;i++)
scanf("%s",mp[i]+);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(mp[i][j]=='.'&& vis[i][j]==)
{
num=; //这个块 有多大
flag=;
cnt++; //有几个联通块
dfs(i,j);
if(flag==) {
nn++;
v[nn].first=cnt;
v[nn].second=num;
}
}
}
}
sort(v+,v++nn,cmp); //升序
int ans=; //需要填的所有块的总大小 即总cell数
int u=;
int V=nn;
while(V>k)
{
V--;
ans+=v[u].second;
tian[ v[u].first ]=;//填的第几块连通的区域标记为1
u++;
} cout<<ans<<endl;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if( tian[ vis[i][j] ] ) //如果填的第vis[i][j]块的区域标记为1
cout<<'*'; //就填了
else cout<<mp[i][j];
}
cout<<endl;
}
}
BFS:
第一次先把湖找出来,并记录湖的大小,然后存在结构体node中,最后sort一下,把要填的湖再bfs一下把这个湖变成陆地。
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=2e5+; int n,m,k;
char mp[][];
int vis[][]={} ,vis2[][]={};
int dx[]={,-,,},dy[]={,,,-};
int nn=,cnt=,flag=,num=,ans=; struct node
{
int x,y,s;
}a,b,c[];
queue<node>q; bool cmp(node u,node v)
{
return u.s<v.s;
} void bfs(int nx,int ny)
{
a.x=nx;
a.y=ny;
vis[a.x][a.y]=;
if(a.x==||a.y==||a.x==n||a.y==m) flag=; //是海不是湖
q.push(a);
while(!q.empty())
{
a=q.front(); q.pop();
for(int i=;i<;i++)
{
b.x=a.x+dx[i];
b.y=a.y+dy[i];
if(b.x<||b.y<||b.x>n||b.y>m||mp[b.x][b.y]!='.'|| vis[b.x][b.y] )
continue;
vis[b.x][b.y]=; if(b.x==||b.y==||b.x==n||b.y==m) flag=; //是海不是湖
num++;
//cout<<num<<endl; q.push(b);
}
}
} void bfs2(int nx,int ny)
{
while(!q.empty()) q.pop();
a.x=nx;
a.y=ny;
vis2[a.x][a.y]=;
mp[a.x][a.y]='*';
ans++; //总数++
q.push(a);
while(!q.empty())
{
a=q.front(); q.pop();
for(int i=;i<;i++)
{
b.x=a.x+dx[i];
b.y=a.y+dy[i];
if(b.x<||b.y<||b.x>n||b.y>m||mp[b.x][b.y]!='.'|| vis2[b.x][b.y] )
continue; mp[b.x][b.y]='*';
vis2[b.x][b.y]=;
ans++;
q.push(b);
}
}
} int main()
{
cin>>n>>m>>k;
for(int i=;i<=n;i++)
scanf("%s",mp[i]+);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='.' && !vis[i][j])
{
num=;
flag=;
bfs(i,j);
if(!flag)
{
nn++;
c[nn].x=i; c[nn].y=j ; c[nn].s=num;
//cout<<i<<' '<<j<<endl;
//cout<<num<<endl;
//记录原始坐标和 这个块的格子数
}
}
}
}
sort(c+,c++nn,cmp); //升序 for(int i=;i<=nn-k ;i++) //开始填
{
bfs2(c[i].x, c[i].y); //这里开始的 一定是'.'
} cout<<ans<<endl;
for(int i=;i<=n;i++)
printf("%s\n",mp[i]+);
}
D. Lakes in Berland (DFS或者BFS +连通块的更多相关文章
- DFS or BFS --- 连通块
Oil Deposits Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 Descrip ...
- CF723D. Lakes in Berland[DFS floodfill]
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 723D Lakes in Berland (dfs搜索)
题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- 经典DFS问题 oilland 连通块
#include "iostream" #include "cstdio" using namespace std; ][]={{,},{,-},{,},{-, ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
随机推荐
- 解决Maven依赖本地仓库eclipse报错的问题
一.应用场景 有时候项目报红色的感叹号错误也是由于项目中没有导入相关jar报导致报错 为了使用maven强大的包依赖管理和项目管理功能,故在项目中使用maven2作为项目建构工具. 但是我的项目在内网 ...
- vue-cli2和vue-cli3同时存在
https://blog.csdn.net/Jioho_chen/article/details/90455778 win下 vue-cli2 和 vue-cli3 并存,一起使用开局一张图,内容慢慢 ...
- selenium 获取不了标签文本的解决方法
selenium 获取不了标签文本的解决方法 ------ 即driver.find_element_by_xxx().text() 为空的解决办法 如果得到的文本只为空,而非我们期望的baidu,那 ...
- ng接口API开发文档
接口版本:v1 接口协议:请商户对接使用对应的转账接口API和免转接口API,商户只能使用菜单对应的API,否则接口会调用失败.左侧菜单未注明的接口免转钱包和转账钱包可以共同使用所有采集均按照北京时间 ...
- 关于AES加密,以及各种分组加密
http://blog.csdn.net/searchsun/article/details/2516191
- [转帖]MMU内存管理单元
MMU内存管理单元 https://www.cnblogs.com/alantu2018/p/9002309.html 之前对这一块一直不理解 最近学习了点 CPU time slice 以及 con ...
- Jmeter CSV操作
统计行号列号 import java.io.BufferedReader; import java.io.FileReader; import java.io.File; print("== ...
- vue图片点击放大功能
因项目需求(ui框架element-ui),需要实现图片的点击放大,还要能旋转以及上下切换.当时第一反应,element-ui好像没有这样的组件,就想过自己写,但是那个旋转翻页上下切换感觉有点麻烦,不 ...
- 路由 router-view
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...
- swagger 爬坑记
Swagger 的好处不用我多说,但是一不小心可能就被坑……今天下午就被上了一课,废话不多说,直接上代码(图) 实体类: 好像没啥问题,对吧? 但是,在http://localhost:8080/sw ...