codeforce375div2-D. Lakes in Berland 搜索
题意与解释:这道题就是求图中被围起来的点群,问最少去掉几个点,可以使得孤立的点群数目为K;
因为自己写的代码又长又had bugs。
我自己写的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 搜索的更多相关文章
- 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 ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 × ...
- 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 ...
- 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 ...
- codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...
- 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 ...
- 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 ...
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 【linux日志】【日志分析】linux系统各日志文件的含义
前段时间太忙,没有来得及管博客,最近时间充裕了,开始更新博客. 因为最近在看linux日志相关内容,把心得分享给大家 linux系统日志文件默认存放路径/var/log/ ls查看此路径下有哪些日志文 ...
- Apache之——多虚拟主机多站点配置的两种实现方案
Apache中配置多主机多站点,可以通过两种方式实现: 将同一个域名的不同端口映射到不同的虚拟主机,不同端口映射到不同的站点: 将同一个端口映射成不同的域名,不同的域名映射到不同的站点. 我们只需要修 ...
- cinder支持nfs快照
[问题描述] cinder后端设置为NFS,磁盘创建快照失败. 日志里面发现了这个错误: VolumeDriverException: Volume driver reported an error: ...
- C# Quartz结合控制台实现定时任务
前言: Quartz一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,基于C#写成,可应用于winform.asp.net.asp.net core应用中.提 ...
- React Native-路由跳转
搭建完RN开发环境后(搭建方式可查看https://www.cnblogs.com/luoyihao/p/11178377.html),要实现多个页面之间的跳转. 1.这时需要安装react-navi ...
- Python.append()与Python.extend()的区别
lst=[1,2] >>>[1,2] lst.append([3,4]) >>>[1, 2, [3, 4]] lst.extend([3,4]) >>& ...
- 怎么把PicPick设置成中文版?
1.首先打开软件 2.在File文件中中点击能看到Program Options这一选项,单击打开 3.右下方有个Language选项,改成简体中文
- Zookeeper_阅读源码第一步_在 IDE 里启动 zkServer(集群版)
上篇文章Zookeeper_阅读源码第一步_在 IDE 里启动 zkServer(单机版)讲了在 idea 里以单机的方式启动zookeeper,这篇介绍一下以集群的方式启动. 集群方式启动,才会真正 ...
- (三)(1)线程间通信---wait和notify的使用
这篇博客记录线程间通信相关api使用以及理解. 首先第一点,我之前的博客里的线程之间也是通信的,但是他们的通信是建立在访问的是同一个变量上的,相当于是变量.数据层面上的通信,而下面要讲的是线程层面上的 ...
- Python中的for else 和while else的用法
for else 在for 循环中,如果没有从任何一个break中退出,则会执行和for对应的else,只要从break中退出了,则else部分不执行. while else while 后面的els ...