题目链接:http://codeforces.com/problemset/problem/377/A

题解:

有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然为连通的。把问题反过来,其实就是求tot-k的连通图。dfs:在搜索过的空格中做个标记,同时更新连通个数。

代码如下:

 #include<cstdio>//hdu3183 CodeForces 377A dfs
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long using namespace std; int n,m,k,sum,vis[][],path[][];
char maze[][]; int dfs(int i, int j)
{
if(sum==k) return ;
if(i< || i>n || j< || j>m) return ;
if(maze[i][j]!='.' || vis[i][j]) return ; sum++;
vis[i][j] = ;
path[i][j] = ;
if(dfs(i-,j)) return ;
if(dfs(i,j-)) return ;
if(dfs(i+,j)) return ;
if(dfs(i,j+)) return ;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
k = -k;
for(int i = ; i<=n; i++)
{
getchar();
for(int j = ; j<=m; j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='.') k++;
}
} int B = ;
memset(vis,,sizeof(vis));
for(int i = ; !B && i<=n; i++)
for(int j = ; j<=m; j++)
{
if(maze[i][j]=='.' && !vis[i][j])
{
sum = ;
memset(path,,sizeof(path));
if(dfs(i,j))
{
B = ;//双重循环,要加多个判断
break;
}
}
} for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(maze[i][j]=='.')
{
if(path[i][j]) putchar('.');
else putchar('X');
} else putchar(maze[i][j]);
if(j==m) putchar('\n');
}
return ;
}

Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)的更多相关文章

  1. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

  2. Codeforces Round #222 (Div. 1) (ABCDE)

    377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...

  3. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  5. Codeforces Round #290 (Div. 2) B (dfs)

    题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...

  6. Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp

    题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...

  7. Codeforces Round #222 (Div. 1) D. Developing Game 扫描线

    D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...

  8. Codeforces Round #222 (Div. 1) C. Captains Mode 状压

    C. Captains Mode 题目连接: http://codeforces.com/contest/377/problem/C Description Kostya is a progamer ...

  9. Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树

    B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...

随机推荐

  1. Xamarin.Forms特殊的视图BoxView

    Xamarin.Forms特殊的视图BoxView   BoxView是Xamarin.Forms比较特殊的视图.该视图构建非常简单,其作用也很单一.它的作用就是构成一个特定颜色的色块.在界面设计中, ...

  2. bzoj 3328 : PYXFIB

    Discription Input 第一行一个正整数,表示数据组数据 ,接下来T行每行三个正整数N,K,P Output T行,每行输出一个整数,表示结果 Sample Input 1 1 2 3 S ...

  3. Java ArrayList 详解

    只记录目前为止关注的.JDK1.8 一.基础属性 1.1 内部参数 //空存储实例.直接new ArrayList()便是以该空数组作为实例 private static final Object[] ...

  4. Java开发者使用C++写程序踩的坑

    笔者是一个很矛盾的人.平时用Java.但是一开始学习的时候学的是汇编语言,而且对C语言也很熟悉.为什么不学C++呢?是因为我可以完全用Java的编码规范去写C++.因此我不需要了解更多的诸如C++的命 ...

  5. Springboot的Bean的Scope

    这周在项目中遇到这样一个Bug,代码大致是这样的,有一个LogEntity日志类,里面有一个InnerLog负责存储每次请求的RPCInfo相关信息, 每次请求的时候会把RPC相关信息加入到Inner ...

  6. 小R与手机

    Description 小R有n部手机,为了便于管理,他对一些手机设置了"呼叫转移"的功能. 具体来说,第 i(1≤i≤n) 部手机有个参数 ai(0≤ai≤n,ai≠i) .若 ...

  7. window 驱动开发

    http://blog.csdn.net/chenyujing1234/article/category/1147469/5

  8. Android传统View动画与Property动画基础及比较

    前言:关于动画方面的知识也整理一段时间了,如题,这篇文章简单的介绍了View和Property动画的概念,如何在项目中创建资源文件,以及如何在代码中使用它们,本次整理动画的重点放在了Property动 ...

  9. 【postman】postman测试API报错如下:TypeError: Failed to execute 'fetch' on 'Window': Invalid value 对中文支持不好

    使用postman测试APi的时候,因为系统需要在header部带上登录用户的信息,所以 如下: 然后测试报错如下:TypeError: Failed to execute 'fetch' on 'W ...

  10. http 错误代码一览表

    http协议一些常见的状态码为: 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分 ...