codeforce375div2-D. Lakes in Berland 搜索

Lakes in Berland

题意与解释:这道题就是求图中被围起来的点群,问最少去掉几个点,可以使得孤立的点群数目为K;

      因为自己写的代码又长又had bugs。

      我自己写的bfs,想着是先染色,后期在考虑这个颜色要不要留。

      第一个bug点是next的点写不对,写了两个nx,应该是一个nx,ny。

      第二个bug,是自己bfs到边界后就直接return了,这样就导致了,有部分点实际上是联通边界的,但是直接return,导致没标记的点出现在下一次的bfs中。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
// #pragma comment(linker, "/STACK:10240000000,10240000000")//扩栈,要用c++交,用g++交并没有什么卵用。。
typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;

#define fi first
#define se second

#define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf  = 0x3f3f3f3f;
template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
///*-----------------show time----------------*/
            const int maxn = 55;
            int mp[maxn][maxn],col[maxn][maxn],sp[maxn][maxn];
            int book[maxn][maxn];
            string g[maxn];
            // int a[3000];
            int nxt[5][5] {
                {1,0},
                {0,1},
                {-1,0},
                {0,-1}
            };
            int n,m,k;
            queue<pii>q;
            int bfs(int x,int y,int tug){
                    int mx = 1;
                    while(!q.empty())q.pop();
                    q.push(make_pair(x,y));
                    col[x][y] = tug;
                   // sp[x][y] = 1;
                    book[x][y] = 1;
                    while(!q.empty()){
                        int tx = q.front().fi;
                        int ty = q.front().se;
                        // cout<<tug<<"###"<<tx<<" "<<ty<<endl;
                        q.pop();
                        for(int i=0; i<=3; i++){
                            int nx = tx + nxt[i][0];
                            int ny = ty + nxt[i][1];  //这里ty 写成tx
                            if(nx < 0 || nx >= n || ny < 0 || ny >= m)continue;
                            if(mp[nx][ny] == 0)continue;
                            if(mp[nx][ny] == 1 && book[nx][ny] != 1){

                                col[nx][ny] = tug;
                            //    sp[nx][ny] = sp[tx][ty] + 1;

                                book[nx][ny] = 1;
                                mx++;
                                if(nx == 0||nx == n-1||ny == 0||ny == m-1){
                                    mx = inf;                  //切莫不要直接return!
                                }
                                q.push(make_pair(nx,ny));
                            }
                        }
                        // debug(q.size());
                    }
                    return mx;
            }

            struct node{
                    int val;
                    int se;
            }a[3000];

            bool cmp(node a,node b){
                return a.val < b.val;
            }

            int shak[3000];
int main(){

            cin>>n>>m>>k;
            for(int i=0; i<n; i++){
                cin>>g[i];
                for(int j=0; j<m; j++){
                    if(g[i][j]==‘*‘) mp[i][j] = 0;
                    else mp[i][j] = 1;
                }
            }

            int tot = 0,  cc = 0;
            for(int i = 1; i<n-1; i++){
                for(int j = 1; j<m-1 ;j++){
                    if(book[i][j]!=1 && mp[i][j]){
                        cc++;
                        int d = bfs(i,j,cc);
                        if(d<inf){
                            tot++;
                            a[tot].val = d;
                            a[tot].se = cc;
                        }
                    }
                }
            }

            sort(a+1,a+1+tot,cmp);
            int sa = tot - k;
            int ans = 0;
            for(int i=1; i<=sa; i++){
                if(a[i].val < inf){
                    ans += a[i].val;
                    shak[a[i].se] = 1;
                }
            }

            printf("%d\n",ans);
            // debug(col[1][2]);
            for(int i=0; i<n; i++){
                for(int j=0;j<m; j++){
                    if(mp[i][j]==1)
                    {
                        if(shak[col[i][j]] == 1)
                            cout<<"*";
                        else cout<<".";
                    }
                    else cout<<"*";
                }
                cout<<endl;
            }
            return 0;
}

BFS-反思

原文地址:https://www.cnblogs.com/ckxkexing/p/9338726.html

时间: 2024-10-06 21:10:52

codeforce375div2-D. Lakes in Berland 搜索的相关文章

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

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

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)

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. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来,第二次去填陆地,选少的进行填. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdli

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

【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

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<qu