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