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 在 ...
随机推荐
- Python:dict用法
dict全称dictionary,使用键-值(key-value)存储,有极快的查找速度. 以下整理几种常用的dict用法 定义 空dict >>> dict={} 普通dict & ...
- idea安装
- MD5加密代码
import java.security.MessageDigest;public class MD5_tes { public final static String MD5(String s){ ...
- 6、HTML5表单提交和PHP环境搭建
---恢复内容开始--- 1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>he ...
- ZTE and TP-Link RomPager - DoS Exploit
#!/usr/bin/env python # -*- coding: utf-8 -*- # Exploit Title: ZTE and TP-Link RomPager DoS Exploit ...
- 在VMware中安装ubuntu出现菜单栏无法显示的情况
在VMware中安装ubuntu出现菜单栏无法显示的情况 其实这个问题的原因时由于VMware中enable了3D图形加速界面,只需要shutdown当前运行的虚拟机,然后在虚拟机,设置,显示器,3D ...
- java 排序
class Employee { private String name; private String id; private String salary; public static void m ...
- php截取utf-8中文字符串乱码的解决方法
/** * PHP截取UTF-8字符串,解决半字符问题. * 英文.数字(半角)为1字节(8位),中文(全角)为2字节 * @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串 ...
- react-native start 运行流程
在CMD下键入 C:\Node_JS\MyAwesomeProject>react-native start 运行流程: C:\Users\Grart\AppData\Roaming\npm\r ...
- MS SQL提示列名 'Y' 无效的原因及解决办法
在作项目写MS SQL 存储过程时,需拼接SQL语句字符串,其中有单字符变量,如下图: 如上图执行存储过程是提示“列名‘Y’无效”.经反复测试,原因在用单字符变量连接SQL字符串是必须在引用变量前后各 ...