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. PyQt4 在Windows下安装

    快来加入群[python爬虫交流群](群号570070796),发现精彩内容.     首先在网上下载sip文件下载完之后解压, 在Windows的开始菜单栏中进入sip的解压目录下:   在目录下面 ...

  2. Java枚举类型 enum

    定义 An enum type is a special data type that enables for a variable to be a set of predefined constan ...

  3. ASP.NET Core 框架本质学习

    本文作为学习过程中的一个记录. 学习文章地址: https://www.cnblogs.com/artech/p/inside-asp-net-core-framework.html 一. ASP.N ...

  4. Docker笔记(八):数据管理

    前面(哪个前面我也忘了)有说过,如果我们需要对数据进行持久化保存,不应使其存储在容器中,因为容器中的数据会随着容器的删除而丢失,而因通过将数据存储于宿主机文件系统的形式来持久化.在Docker容器中管 ...

  5. Streaming+Sparksql使用sql实时分析 rabbitmq+mongodb+hive

    SparkConf sparkConf = new SparkConf()//此处使用一个链接切记使用一个链接否则汇报有多个sparkcontext错误 .setAppName("Spark ...

  6. mpvue微信小程序项目踩坑记录

    1.mpvue入门教程, http://mpvue.com/mpvue/quickstart.html # . 先检查下 Node.js 是否安装成功 $ node -v v8.9.0 $ npm - ...

  7. nginx 使用HTTPS协议-SSL证书模块报错解决-附nginx安装 : [emerg] the "ssl" parameter requires ngx_http_ssl_module in nginx.c

    Linux系统下ngnix使用HTTPS协议启动报错: nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_modul ...

  8. 在linux系统下安装mysql详解,以及远程调用连接不上mysql的解决方法。

    步骤: 1)查看CentOS自带的mysql 输入 rpm -qa | grep mysql 2)将自带的mysql卸载 3)上传Mysql的安装包到linux 4)安装mysql的依赖(不是必须) ...

  9. appium输入法踩坑解决方案-----中文乱码及输入法搜索无法点击

    一.appium1.7.1 遇到的坑: 1. 在写安卓的搜索用例脚本时,发现输入内容后,搜索出现在输入法键盘原来的确认位置,定位不到手机自带输入法的"搜索"键: 2. 传入中文搜索 ...

  10. 简单了解一下事件循环(Event Loop)

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...