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 在 ...
随机推荐
- 获取checkbox后面的文本内容
http://alygle.blog.51cto.com/1922399/669040 <head> <meta http-equiv="Content-Type" ...
- js面向对象的封装方法,【案例】
封装方法: /** * @矩形canvas库 * @authors Shimily (275766400@qq.com) * @date 2016-12-28 10:30:51 * @version ...
- windbg无法下载符号文件
symbol file path: srv*d:\symbolslocal*http://msdl.microsoft.com/download/symbols 即使设置是对的,但我用.reload, ...
- JQUERY MOBILE 中文API站 和 官方论坛
中文API站:http://www.jqmapi.com/api1.2/preview/quickstartquide.html 官方论坛:http://bbs.phonegapcn.com/foru ...
- RSA加密,应用授权及MSSQL备份与还原
01.QBRSA加解密处理 --> a.利用 RSA密钥生成器生成密钥(e,n,d) [e,n]为私钥, [d,n]为公钥 b.正向加密: 用私钥加密,用公钥解密 c.反向加密: 用公钥加密 ...
- jQuery设置聚焦并使光标位置在文字最后
var editor = document.getElementById('btn'); editor.onfocus = function () { window.setTimeout(functi ...
- Jquery的各种验证
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- jstl_fmt
<fmt:formatDate value="${isoDate}" type="both"/>2004-5-31 23:59:59<fmt: ...
- k8s pv
这个文档描述当前在k8s中PersistentVolumes的使用. 我们建议和volume一起进行了解 Introduction 管理存储和管理计算是截然不同的问题. 持久存储子系统对用 ...
- 微信APP支付服务端开发Java版(一)
一.准备工作 去微信开发者中心下载(扫码支付,里面的大部分代码是可以用的) https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=11 ...