Codeforces Round #375 (Div. 2) D 连通块
来源:互联网 发布:初榨椰子油 知乎 编辑:程序博客网 时间:2024/05/16 11:36
题目传送门:http://codeforces.com/contest/723/problem/D
题意:给一张N*M的图,图被ocean包围,'*'代表land,‘.’代表cell,相邻的所有cell组成lake,如果有cell靠近海,则其所有相连的cell不能组成lake,给一个K,问最少进行多少次操作将cell变成land,才能让图只有K个lake(k一定小于等于现有lake)
思路:就是连通图,比赛时不如先做这一道。但赛后做还是wa了n发,思路没错就是有些细节马虎了,宛如一个zz = =
代码如下:
#include <iostream>#include <algorithm>#include <cstring>#include <stdio.h>#include <string>#include <cmath>#include <queue>#include <set>#include <map>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define ull unsigned long long#define mem(n,v) memset(n,v,sizeof(n))#define MAX 55#define MAXN 3000#define PI 3.1415926#define E 2.718281828459#define opnin freopen("text.in.txt","r",stdin)#define opnout freopen("text.out.txt","w",stdout)#define clsin fclose(stdin)#define clsout fclose(stdout)#define haha1 cout << "haha1"<< endl#define haha2 cout << "haha2"<< endl#define haha3 cout << "haha3"<< endlconst int INF = 0x3f3f3f3f;const ll INFF = 0x3f3f3f3f3f3f3f3f;const double pi = 3.141592653589793;const double inf = 1e18;const double eps = 1e-8;const ll mod = 1e9+7;const ull mx = 133333331;int Map[MAX][MAX];int vis[MAX][MAX];int n,m,k;struct llake{ int x,y,Size; llake(int a = 0,int b = 0,int S = 0) : x(a),y(b),Size(S) {} friend bool operator < (llake a,llake b) { return a.Size < b.Size; }}lake[MAXN];int flag = false;int dfs(int x,int y){ int Size = 1; vis[x][y] = 1; if(x == 1 || x == n || y == 1 || y == m){ flag = true; } if(x - 1 >= 1 && Map[x-1][y] == '.' && !vis[x-1][y] ) Size += dfs(x-1,y); if(x + 1 <= n && Map[x+1][y] == '.' && !vis[x+1][y] ) Size += dfs(x+1,y); if(y - 1 >= 1 && Map[x][y-1] == '.' && !vis[x][y-1] ) Size += dfs(x,y-1); if(y + 1 <= m && Map[x][y+1] == '.' && !vis[x][y+1] ) Size += dfs(x,y+1); return Size;}void dfs1(int x,int y){ Map[x][y] = '*'; if(x - 1 > 1 && Map[x-1][y] == '.') dfs1(x-1,y); if(x + 1 < n && Map[x+1][y] == '.') dfs1(x+1,y); if(y - 1 > 1 && Map[x][y-1] == '.') dfs1(x,y-1); if(y + 1 < m && Map[x][y+1] == '.') dfs1(x,y+1);}int main(){ cin >> n >> m >> k; mem(Map,0); mem(vis,0); mem(lake,0); int num = 1; getchar(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%c",&Map[i][j]); } getchar(); } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(Map[i][j] == '.' && !vis[i][j]){ flag = false; int temp = dfs(i,j); if(!flag && temp > 0){ lake[num].x = i; lake[num].y = j; lake[num++].Size = temp; } } } } int cnt = 0; int num_temp = num; sort(lake+1,lake+num+1); for(int i=1;i<=num;i++){ if(num_temp == k) break; cnt += lake[i].Size; num_temp--; dfs1(lake[i].x,lake[i].y); } cout << cnt << endl; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ printf("%c",Map[i][j]); } printf("\n"); } return 0;}
0 0
- Codeforces Round #375 (Div. 2) D 连通块
- Codeforces Round #375 (Div. 2) D
- Codeforces Round #375 (Div. 2) D bfs
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
- Codeforces Round #286 (Div. 2) D. Mr. Kitayuta's Technology 强连通分量 有向图求环
- [Codeforces Round #369 (Div. 2)D. Directed Roads]Tarjan强连通分量+组合计数
- Codeforces Round #286 div.2 D 505D. Mr. Kitayuta's Technology【强连通分量,弱联通分量】
- Codeforces Round #286 div.2 D 505D. Mr. Kitayuta's Technology【强连通分量,弱联通分量】
- Codeforces Round #375 (Div. 2)【A,B【模拟】,D【DFS】】
- Codeforces Round #375 (Div. 2)(A~D)
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
- Codeforces Round #103 (Div. 2) D
- Codeforces Round #104 (Div. 2) D
- Codeforces Round #105 (Div. 2) D
- Codeforces Round #139 (Div. 2) D. Snake
- 制作全新OS X EI Capitan安装U盘
- (坑)网络流24题
- android网络连接使用GET方式请求服务器时的setDoOutput(true)惹的祸
- 硬件串行接口总结
- Zabbix-3.0.3使用自带模板监控MySQL
- Codeforces Round #375 (Div. 2) D 连通块
- Android中Looper类
- 习题8.10
- python 学习笔记(4) python函数和模块
- Swift学习记录 -- 16.微博项目初体验
- PCA算法:从一组照片中获取特征脸(特征向量)
- linux下ScrollLock键盘灯不亮
- 欢迎使用CSDN-markdown编辑器
- CentOS 6.8 配置GO语言开发环境