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; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',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 = ;
int mp[maxn][maxn],col[maxn][maxn],sp[maxn][maxn];
int book[maxn][maxn];
string g[maxn];
// int a[3000];
int nxt[][] {
{,},
{,},
{-,},
{,-}
};
int n,m,k;
queue<pii>q;
int bfs(int x,int y,int tug){
int mx = ;
while(!q.empty())q.pop();
q.push(make_pair(x,y));
col[x][y] = tug;
// sp[x][y] = 1;
book[x][y] = ;
while(!q.empty()){
int tx = q.front().fi;
int ty = q.front().se;
// cout<<tug<<"###"<<tx<<" "<<ty<<endl;
q.pop();
for(int i=; i<=; i++){
int nx = tx + nxt[i][];
int ny = ty + nxt[i][]; //这里ty 写成tx
if(nx < || nx >= n || ny < || ny >= m)continue;
if(mp[nx][ny] == )continue;
if(mp[nx][ny] == && book[nx][ny] != ){ col[nx][ny] = tug;
// sp[nx][ny] = sp[tx][ty] + 1; book[nx][ny] = ;
mx++;
if(nx == ||nx == n-||ny == ||ny == m-){
mx = inf; //切莫不要直接return!
}
q.push(make_pair(nx,ny));
}
}
// debug(q.size());
}
return mx;
} struct node{
int val;
int se;
}a[]; bool cmp(node a,node b){
return a.val < b.val;
} int shak[];
int main(){ cin>>n>>m>>k;
for(int i=; i<n; i++){
cin>>g[i];
for(int j=; j<m; j++){
if(g[i][j]=='*') mp[i][j] = ;
else mp[i][j] = ;
}
} int tot = , cc = ;
for(int i = ; i<n-; i++){
for(int j = ; j<m- ;j++){
if(book[i][j]!= && mp[i][j]){
cc++;
int d = bfs(i,j,cc);
if(d<inf){
tot++;
a[tot].val = d;
a[tot].se = cc;
}
}
}
} sort(a+,a++tot,cmp);
int sa = tot - k;
int ans = ;
for(int i=; i<=sa; i++){
if(a[i].val < inf){
ans += a[i].val;
shak[a[i].se] = ;
}
} printf("%d\n",ans);
// debug(col[1][2]);
for(int i=; i<n; i++){
for(int j=;j<m; j++){
if(mp[i][j]==)
{
if(shak[col[i][j]] == )
cout<<"*";
else cout<<".";
}
else cout<<"*";
}
cout<<endl;
}
return ;
}

BFS-反思

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

  1. 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 cel ...

  2. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 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 × ...

  4. 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 ...

  5. 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 ...

  6. codeforces723 D. Lakes in Berland(并查集)

    题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...

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

  8. 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 ...

  9. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 解决微信小程序开发者工具输入框焦点问题

    Windows10笔记本上运行微信小程序开发者工具,输入框(input,textarea)没有焦点,只能在真机调试,效率太低.后来发现是Window10对笔记本高分屏支持不好,要DPI缩放,导致兼容性 ...

  2. ContentProvider 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  3. xml的四种解析方式(转载)

    众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...

  4. MVP架构下解决 RxJava 自动解绑问题

    背景 MVP 模式下使用 RxJava 处理网络访问的回调,当数据返回时 Presenter 调用绑定的 View 的方法. 定义 BasePresenter 如下: public class Bas ...

  5. http客户端-性能比较系列-第二篇-多线程

    系列文章: 单线程性能测试:https://www.cnblogs.com/victor2302/p/11077208.html 多线程性能测试:https://www.cnblogs.com/vic ...

  6. Java虚拟机(二)-对象创建

    这一篇大致说明一下,对象在Java堆中对象分配.内存布局以及访问定位 1.对象的创建 虚拟机在遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引 ...

  7. 【0808 | Day 11】文件的高级应用/修改以及函数的定义/使用/参数

    文件的高级应用 一.三种模式 'r+'模式 with open('test.py','r',encoding = 'utf8') as fr: print(fr.writable()) fr.writ ...

  8. TP5使用API时不可预知的内部异常

    最常见的错误形式例如 controller不存在或者 action不存在之类的 我们第一时间想到的 就是 使用 try{}catch(){} 来捕获 例如: /** * show方法在common里定 ...

  9. 链表:如何实现LRU缓存淘汰算法?

    缓存淘汰策略: FIFO:先入先出策略 LFU:最少使用策略 LRU:最近最少使用策略   链表的数据结构: 可以看到,数组需要连续的内存空间,当内存空间充足但不连续时,也会申请失败触发GC,链表则可 ...

  10. ASP.NET Core[源码分析篇] - WebHost

    _configureServicesDelegates的承接 在[ASP.NET Core[源码分析篇] - Startup]这篇文章中,我们得知了目前为止(UseStartup),所有的动作都是在_ ...