CodeForces 723D Lakes in Berland

连通块。

求出连通块,排序即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-10;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c))
    {
        x = x * 10 + c - ‘0‘;
        c = getchar();
    }
}

struct X
{
    int id,c;
}s[200000],ss[200000];
int sz,cnt,n,m,k;
char Map[60][60];
int f[60][60],t[100000],h[100000];

bool check(int a,int b)
{
    if(a<0||a>=n) return 0;
    if(b<0||b>=m) return 0;
    if(Map[a][b]==‘*‘) return 0;
    if(f[a][b]!=0) return 0;
    return 1;
}

void dfs(int a,int b)
{
    f[a][b]=sz;
    s[sz].c++;
    if(check(a+1,b)) dfs(a+1,b);
    if(check(a-1,b)) dfs(a-1,b);
    if(check(a,b+1)) dfs(a,b+1);
    if(check(a,b-1)) dfs(a,b-1);
}

bool cmp(X a, X b)
{
    return a.c<b.c;
}

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++) cin>>Map[i];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(f[i][j]!=0) continue;
            if(Map[i][j]==‘*‘) continue;

            sz++; s[sz].id=sz; dfs(i,j); h[sz]=1;
        }
    }

    for(int i=0;i<n;i++)
    {
        if(f[i][0]!=0) h[f[i][0]]=0;
        if(f[i][m-1]!=0) h[f[i][m-1]]=0;
    }

    for(int j=0;j<m;j++)
    {
        if(f[0][j]!=0) h[f[0][j]]=0;
        if(f[n-1][j]!=0) h[f[n-1][j]]=0;
    }

    for(int i=1;i<=sz;i++)
    {
        if(h[i]==0) continue;
         cnt++;
        ss[cnt].c=s[i].c;
        ss[cnt].id=s[i].id;
    }

    sort(ss+1,ss+1+cnt,cmp);
    /*
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d ",f[i][j]);
        }
        cout<<endl;
    }
    */

    int ans=0;
    for(int i=1;i<=cnt-k;i++)
    {
        ans=ans+ss[i].c;
        t[ss[i].id]=1;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(t[f[i][j]]==1)
            {
                Map[i][j]=‘*‘;
            }
        }
    }

    printf("%d\n",ans);
    for(int i=0;i<n;i++) cout<<Map[i]<<endl;

    return 0;
}
时间: 2024-10-14 05:57:34

CodeForces 723D Lakes in Berland的相关文章

CodeForces 723D Lakes in Berland (dfs搜索)

题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来,第二次去填陆地,选少的进行填. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdli

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

Codeforces Round #375 (Div. 2) D. Lakes in Berland DFS

D. Lakes in Berland 链接: http://codeforces.com/problemset/problem/723/D 题意 给你一个n/*m的矩阵,然后你们有不少于k条湖泊,然后你需要使得一些湖泊变成陆地,使得湖泊的数量恰好等于k,问你至少填多少个水. 湖泊不与外界相邻. 题解: 直接dfs搜出每一条湖泊,然后放在优先队列里,从小到大去填满就好了. 代码: 1 #include<iostream> 2 #include<queue> 3 #include&l

cf723d Lakes in Berland

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 connect

codeforces723 D. Lakes in Berland(并查集)

题目链接:codeforces723 D. Lakes in Berland 房教教我的,我按他思路写了好久才过,我好菜啊 房教的博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<algorithm> 5 #define CLR(a,b) memset((a),(b)

codeforce375div2-D. Lakes in Berland 搜索

Lakes in Berland 题意与解释:这道题就是求图中被围起来的点群,问最少去掉几个点,可以使得孤立的点群数目为K; 因为自己写的代码又长又had bugs. 我自己写的bfs,想着是先染色,后期在考虑这个颜色要不要留. 第一个bug点是next的点写不对,写了两个nx,应该是一个nx,ny. 第二个bug,是自己bfs到边界后就直接return了,这样就导致了,有部分点实际上是联通边界的,但是直接return,导致没标记的点出现在下一次的bfs中. #include <iostream

CF723D 【Lakes in Berland】

题目链接 题解 CF723D [Lakes in Berland] 首先将边界的水用bfs处理掉 再将中间的每一个湖泊处理出来,存入一个结构体内,结构体里记录湖泊大小和开始点 将湖泊排序从小往大填满,并利用开始点进行bfs改变地图 细节见代码: #include<bits/stdc++.h> using namespace std; int n,m,k; int vis[100][100],mapp[100][100]; int dx[5]={0,-1,0,1,0}; int dy[5]={0

【Codeforces 723D】Lakes in Berland (dfs)

海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按面积排序,若湖的数量为cnt,填掉前cnt-k个湖. http://codeforces.com/problemset/problem/723/D Examples input 5 4 1*****..*******.*..** output 1*****..*********..** input

F - Lakes in Berland(BFS)

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 connect