Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接: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(连通块)的更多相关文章
- Codeforces Round #222 (Div. 1) A. Maze dfs
A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...
- Codeforces Round #222 (Div. 1) (ABCDE)
377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...
- 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 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #290 (Div. 2) B (dfs)
题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...
- Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp
题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...
- Codeforces Round #222 (Div. 1) D. Developing Game 扫描线
D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...
- Codeforces Round #222 (Div. 1) C. Captains Mode 状压
C. Captains Mode 题目连接: http://codeforces.com/contest/377/problem/C Description Kostya is a progamer ...
- 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 ...
随机推荐
- Careercup | Chapter 4
二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树 ...
- CF768
Codeforces Round #406 (Div. 1) A.Berzerk 考虑先手必胜态,一定是先手移动到某一个位置以后,这个位置是后手的必败态 考虑先手必败态,一定是无论先手如何移动,先手所 ...
- Maven修改默认中央仓库
其实Maven的默认仓库是可以修改的.比如使用阿里云的镜像地址等. 修改步骤: 1.打开{M2_HOME}/conf/settings.xml文件,找到mirrors节点,修改如下代码: <mi ...
- xamarin studio 中SpinButton ComBox Splid 鼠标放上去就会自动接收焦点,然后进行数值变化
公司做跨平台项目,用XamarinStudio 开发mac版本,语法还是C#,但是,尼玛XamarinStudio的控件就是坑爹啊. 其他的暂时不累赘,笔者画界面,一堆控件放到一个界面上,当超出屏幕时 ...
- TI C66x DSP 四种内存保护问题 -之- CPU訪问corePac内部资源时的内存保护问题
CPU訪问corePac内部资源(L1.L2)时的内存保护(通过设置内存的訪问权限实现)等问题请參考以下两个blog.已经叙述的非常具体. "TI C66x DSP 系统events及其应用 ...
- h5调用手机照相机
camera.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- Java数据库访问技术
1.Java集合: Collection Map List: 位于 java.util包中. Arraylist 顺序结构.Linkedlist 链表结构 // List<NewsClassif ...
- 无法访问gcr.io的几种解决办法
系列目录 由于一些原因,在国内无法访问gcr.io上的镜像,在安装kubernetes时,很多官方镜像又是都存在gcr.io上,在国内的一些教程中大都使用阿里云的镜像,但是由于阿里云镜像地址更换等原因 ...
- 后端程序员看前端想死(二)进入页面之后js分析
在上一篇中分析了一下以网页的组成.header中引入的乱七八糟的东西,现在进入到js中进行分析了 tuhooo啊,你是要搞前端了么? nonono,好玩,学一下 打开页面之后执行js的几种方法 直接写 ...
- nginx的源代码分析--间接回调机制的使用和类比
nginx使用了间接回调机制.结合upstream机制的使用来说明一下,首先明白几个事实: 1)事实上ngxin和下游client的连接使用的是ngx_http_connection_t,每一个连接相 ...