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. Sring 的 @AliasFor 使用规则

    一.该标签存在的意义 顾名思义 @AliasFor 表示别名,它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义.该标签存在的含义,从网上查发现有个点, 若  自 ...

  2. git之coding.net的使用

    先在Coding上创建个项目     现在是这样,我本地有个项目Project(/Users/huang/Desktop/Project),我想把它上传到刚创建的项目里,以后就用git代码托管.可我之 ...

  3. 进军pc市场 华为剑走偏锋可有戏?

    尽管官方并未正式公布,但在前段时间,华为将要进军PC市场的消息在业内传得沸沸扬扬,据知情人士曝料,其第一款个人电脑将在今年4月上线.而华为将进军PC市场的消息,对其他智能手机厂商来说又意味着什么呢? ...

  4. SpringBoot第一天

    一,SpringBoot 介绍 1,如果使用 Spring 开发一个"HelloWorld"的 web 应用: • 创建一个 web 项目并且导入相关 jar 包.SpringMV ...

  5. 10分钟安装Elasticsearch

    关注公众号 itweknow,回复"ES"获取<Elasticsearch权威指南 中文版>. 最近在尝试着搭建一个ELK(一个开源的实时日志分析平台),而本文所讲的E ...

  6. jQuery学习和知识点总结归纳

    jQuery目前在Web前端开发所占的比重越来越高,在我们jQuery学习和开发的过程中都会去使用.jQuery帮我们解决了浏览器之间JS一些不兼容的地方和简化了原生JS对DOM的操作.下面把PHP程 ...

  7. 【POJ - 3280】Cheapest Palindrome(区间dp)

    Cheapest Palindrome 直接翻译了 Descriptions 给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符 ...

  8. 每天用SpringBoot,还不懂RESTful API返回统一数据格式是怎么实现的?

    上一篇文章RESTful API 返回统一JSON数据格式 说明了 RESTful API 统一返回数据格式问题,这是请求一切正常的情形,这篇文章将说明如何统一处理异常,以及其背后的实现原理,老套路, ...

  9. Vue 路由模块化配置

    博客地址:https://ainyi.com/77 企业运营后台页面很多,路由如若不区分模块化配置,所有路由挤在同一个文件将不好维护,所以路由的配置也要模块化 分享两个解决方案 -- Vue 路由配置 ...

  10. 安利一个免费下载VIP文档神器

    今天安利给大伙一个非非非常好用的可以免费下载VIP文档的下载神器------冰点文库下载器,用过的人都说好.操作简单,小巧轻便,完全免费.支持百度.豆丁.畅享.mbalib.hp009.max.boo ...