codeforces 723D(DFS)
题目链接:http://codeforces.com/problemset/problem/723/D
题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊。问最少填多少块water能使湖泊数量降到k个。
思路:本来最有把握的一次CF,D题小错误一直RE,C题最后也FST了......
先DFS出各湖泊的大小并用其中一个点存在结构体中,最后有num0个湖泊,再按湖泊的大小排序,需要填的湖泊为前n-k小的湖泊,简单DFS一下就好了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[55][55];
bool vis[55][55];
int turnx[10] = {-1,1,0,0};
int turny[10] = {0,0,-1,1};
int n,m,k,num,flag;
struct node
{
int num;
int x;
int y;
}q[3000];
bool cmp(node a,node b)
{
return a.num < b.num;
}
bool in(int x,int y)
{
if(x < 1 || y < 1 || x > n || y > m)
return 0;
return 1;
}
void dfs(int x,int y)
{
if(vis[x][y] || a[x][y] == '*')
return;
num++;
vis[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int nx = x + turnx[i];
int ny = y + turny[i];
if(!in(nx,ny))
{
num = 0;
flag = 0;
continue;
}
if(a[nx][ny] == '*' || vis[nx][ny])
continue;
dfs(nx,ny);
}
}
void dfs1(int x,int y)
{
a[x][y] = '*';
for(int i = 0; i < 4; i++)
{
int nx = x + turnx[i];
int ny = y + turny[i];
if(a[nx][ny] == '*' )
continue;
dfs1(nx,ny);
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
int num0 = 0,ans = 0;
for(int i = 1; i <= n; i++)
scanf("%s",a[i] + 1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
num = 0,flag = 1;
if(!vis[i][j] && a[i][j] == '.')
{
dfs(i,j);
if(flag)
{
q[num0].num = num;
q[num0].x = i;
q[num0].y = j;
num0++;
}
}
}
}
sort(q,q+num0,cmp);
for(int i = 0; i < num0 - k; i++)
ans += q[i].num;
printf("%d\n",ans);
for(int i = 0; i < num0 - k; i++)
dfs1(q[i].x,q[i].y);
for(int i = 1; i <= n; i++)
printf("%s\n",a[i]+1);
return 0;
}
codeforces 723D(DFS)的更多相关文章
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- CodeForces - 95B(DFS)
题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...
- CodeForces - 589J —(DFS)
Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schema ...
- CodeForces - 589J(DFS)
题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...
- Codeforces 761E(DFS)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 115A- Party(DFS)
A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
随机推荐
- sicily 1934. 移动小球
Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...
- MVC5+EF6 入门完整教程四
上篇文章主要讲了如何配置EF, 我们回顾下主要过程: 创建Data Model à 创建Database Context à创建databaseInitializerà配置entityFramewor ...
- UIKit - scrollView缩放、滚动
UIScrollView滚动 三大属性: self.scrollView.pageEnabled = NO 是否分页:n只要将UIScrollView的pageEnabled属性设置为YES,UIS ...
- CSS基础3
1.变形样式 transform : none | <transform-function>,改变元素的大小,透明,旋转角度,扭曲度等.<transform-function> ...
- 艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输)(一)
艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输) 该系统基于开源的networkComms通讯框架,此通讯框架以前是收费的,目前已经免费并开元,作者是英国的,开发时间5年多,框架很稳定. 项 ...
- jquery中select的应用
//得到select项的个数 jQuery.fn.size = function(){ return jQuery(this).get(0).options.length; } //获得选中项的索引 ...
- checkbox
$(document).ready(function(){ var page_id = {/$page_id/}; var location_id = {/$location_id/}; var lo ...
- 怎么将Android studio 的“ build:gradle改低一点”
参考来源:http://bbs.qcloud.com/thread-17193-1-1.html Error:Execution failed for task ':xxxx:compileDebug ...
- Win环境下的文件读写
在win环境下,有许多方法可以对文件进行读写操作,如MFC 中的CFile类,及一些开源的项目如QT中的QFile.开源的好得是可以多平台,而MFC只是微软自家的东西,对于想写跨平台的人,最好不用MF ...
- 将命令添加到shell脚本中然后设置开机自启动
例如开机自启动nginx 编写一个脚本 #vi /usr/local/Monitor_nginx.sh #!/bin/bash if [ "$(ps -ef | grep "ngi ...