codeforce375div2-D. Lakes in Berland 搜索
阿新 • • 發佈:2018-07-20
聯通 string ref lib ios time str pan inline
Lakes in Berland
題意與解釋:這道題就是求圖中被圍起來的點群,問最少去掉幾個點,可以使得孤立的點群數目為K;
因為自己寫的代碼又長又had bugs。
我自己寫的bfs,想著是先染色,後期在考慮這個顏色要不要留。
第一個bug點是next的點寫不對,寫了兩個nx,應該是一個nx,ny。 第二個bug,是自己bfs到邊界後就直接return了,這樣就導致了,有部分點實際上是聯通邊界的,但是直接return,導致沒標記的點出現在下一次的bfs中。#include <iostream> #includeBFS-反思<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; }
codeforce375div2-D. Lakes in Berland 搜索