CodeForces 378C Maze (DFS)
题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案。
分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘
一直填充k个。
因为在搜索的过程中,一直都是vis[][]标记的,所以时间复杂度最多只是搜了所有的边,即500*500*4.
DFS搜索的时间复杂度,取决于边的个数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
char s[maxn][maxn];
int dx[] = {, , , -};
int dy[] = {, -, , };
int vis[maxn][maxn];
int n, m, cnt;
void dfs(int fx, int fy)
{
vis[fx][fy] = ;
for(int i = ; i < ; i++)
{
int a, b;
a = fx+dx[i];
b = fy+dy[i];
if(a>=&&a<=n && b>=&&b<=m){}
else continue;
if(s[a][b]!='.') continue;
if(!vis[a][b])
dfs(a, b);
}
if(cnt)
{
cnt --;
vis[fx][fy] = ;
}
} int main()
{
int i, j;
int fx, fy;
while(~scanf("%d%d%d", &n, &m, &cnt))
{
memset(vis, , sizeof(vis));
memset(s, , sizeof(s));
for(i = ; i <= n; i++)
{
getchar();
for(j = ; j <= m; j++)
{
scanf("%c", &s[i][j]);
if(s[i][j]=='.')
{
fx = i;
fy = j;
}
}
}
dfs(fx, fy); for(i = ; i <= n; i++)
{
for(j = ; j <= m; j++)
{
if(vis[i][j]==)
printf("X");
else
printf("%c", s[i][j]);
}
printf("\n");
}
}
return ;
}
CodeForces 378C 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) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- Codeforces 377A - Maze
A. Maze 题目链接:http://codeforces.com/contest/377/problem/A time limit per test 2 seconds memory limit ...
- codeforces 734E(DFS,树的直径(最长路))
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- codeforces 723D(DFS)
题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...
- Codeforces 123E Maze(树形DP+期望)
[题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...
- HDU 1484 Basic wall maze (dfs + 记忆)
Basic wall maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Military Problem CodeForces 1006E (dfs序)
J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...
随机推荐
- php正则过滤html标签、空格、换行符的代码,提取图片
$descclear = str_replace("r","",$descclear);//过滤换行 $descclear = str_replace(&quo ...
- YARN应用程序的开发步骤
开发基于YARN的应用程序需要开发客户端程序和AppMaster程序: 我们基于程序自带的例子来实现提交application 到YARN的ResourceManger. Distributed Sh ...
- Sqli-labs less 51
Less-51 本关的sql语句为 $sql="SELECT * FROM users ORDER BY '$id'"; 我们此处要进行stacked injection,要 ...
- ASP.NET MVC的处理管线
原文:http://www.cnblogs.com/fzrain/p/3651693.html 下面开始解释各个部分: 路由模块 1.在ASP.NET MVC处理管线中的第一站就是路由模块.当请求到达 ...
- LA 2038
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...
- POJ 1978
#include <iostream> #define MAXN 55 using namespace std; int _m[MAXN]; int tem[MAXN]; void cop ...
- HDU 1796 How many integers can you find (状态压缩 + 容斥原理)
题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...
- js 异步请求封装
1. function ajax(url, onsuccess) { var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ...
- 李洪强iOS开发之多线程编程2-NSOperation
前言 1.上一讲简单介绍了NSThread的使用,虽然也可以实现多线程编程,但是需要我们去管理线程的生命周期,还要考虑线程同步.加锁问题,造成一些性能上的开销.我们也可以配合使用NSOperation ...
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
一.简介: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean 通常包含了大量的属性,很多情况下,对Jav ...