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. 【MySQL】服务无法启动(Mac)

    如图所示: 点击 Start MySQL Server 没反应-- 终端输入 mysql 命令时报错如下: ERROR 2002 (HY000): Can't connect to local MyS ...

  2. SpringBoot RabbitMQ 整合使用

    ![](http://ww2.sinaimg.cn/large/006tNc79ly1g5jjb62t88j30u00gwdi2.jpg) ### 前提 上次写了篇文章,[<SpringBoot ...

  3. Java虚拟机学习笔记(一)之初识

    一:特定 跨平台性.安全性.可移植性. 二:体系机构 Java 体系结构包括四个独立但相关的技术: Java程序设计语言 Java Class文件格式 Java 应用编程接口(API) Java 虚拟 ...

  4. drf之序列化

    在django视图中使用serializer 只是使用serializer类编写API视图,没有用到REST框架 app01下的models.py from django.db import mode ...

  5. Wtm携手LayUI -- .netcore 开源生态我们是认真的!

    经过WTM团队和LayUI团队多次深入协商,双方于2019年7月29日在北京中国国际展览中心正式达成战略合作意向, 双方签署了战略合作框架协议,LayUI团队承诺使用WTM框架的任何项目都可以免费使用 ...

  6. 一文带你彻底理解 JavaScript 原型对象

    一.什么是原型 原型是Javascript中的继承的基础,JavaScript的继承就是基于原型的继承. 1.1 函数的原型对象 在JavaScript中,我们创建一个函数A(就是声明一个函数), 那 ...

  7. Go包管理工具dep

    dep是一个golang依赖管理工具,需要在Go 1.7及更高的版本中使用. 1. 安装 安装dep工具的方式有很多种,如果是mac电脑的话,只需要如下命令: brew install dep 对于L ...

  8. 渐进式web应用开发---使用indexedDB实现ajax本地数据存储(四)

    在前几篇文章中,我们使用service worker一步步优化了我们的页面,现在我们学习使用我们之前的indexedDB, 来缓存我们的ajax请求,第一次访问页面的时候,我们请求ajax,当我们继续 ...

  9. JS鼠标吸粉特效

    HTML <canvas id=canvas></canvas> CSS * { margin: 0; padding: 0; } html { overflow: hidde ...

  10. C#_HttpWebRequest保存cookies模拟登录的方法

    CookieContainer cookies = new CookieContainer(); string url = "http://www.google.com.hk/"; ...