题目链接

题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案。

分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘

一直填充k个。

因为在搜索的过程中,一直都是vis[][]标记的,所以时间复杂度最多只是搜了所有的边,即500*500*4.

DFS搜索的时间复杂度,取决于边的个数。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <queue>
  7. #include <algorithm>
  8. #define LL __int64
  9. const int maxn = +;
  10. using namespace std;
  11. char s[maxn][maxn];
  12. int dx[] = {, , , -};
  13. int dy[] = {, -, , };
  14. int vis[maxn][maxn];
  15. int n, m, cnt;
  16. void dfs(int fx, int fy)
  17. {
  18. vis[fx][fy] = ;
  19. for(int i = ; i < ; i++)
  20. {
  21. int a, b;
  22. a = fx+dx[i];
  23. b = fy+dy[i];
  24. if(a>=&&a<=n && b>=&&b<=m){}
  25. else continue;
  26. if(s[a][b]!='.') continue;
  27. if(!vis[a][b])
  28. dfs(a, b);
  29. }
  30. if(cnt)
  31. {
  32. cnt --;
  33. vis[fx][fy] = ;
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. int i, j;
  40. int fx, fy;
  41. while(~scanf("%d%d%d", &n, &m, &cnt))
  42. {
  43. memset(vis, , sizeof(vis));
  44. memset(s, , sizeof(s));
  45. for(i = ; i <= n; i++)
  46. {
  47. getchar();
  48. for(j = ; j <= m; j++)
  49. {
  50. scanf("%c", &s[i][j]);
  51. if(s[i][j]=='.')
  52. {
  53. fx = i;
  54. fy = j;
  55. }
  56. }
  57. }
  58. dfs(fx, fy);
  59.  
  60. for(i = ; i <= n; i++)
  61. {
  62. for(j = ; j <= m; j++)
  63. {
  64. if(vis[i][j]==)
  65. printf("X");
  66. else
  67. printf("%c", s[i][j]);
  68. }
  69. printf("\n");
  70. }
  71. }
  72. return ;
  73. }

CodeForces 378C 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) Maze —— dfs(连通块)

    题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...

  3. Codeforces 377A - Maze

    A. Maze 题目链接:http://codeforces.com/contest/377/problem/A time limit per test 2 seconds memory limit ...

  4. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  5. codeforces 731C(DFS)

    题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...

  6. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  7. Codeforces 123E Maze(树形DP+期望)

    [题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...

  8. HDU 1484 Basic wall maze (dfs + 记忆)

    Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

随机推荐

  1. 户外物理渗透:终端机,客户端的web测试思路

    现在的客户端界面越做越好看了,很多用到了web技术,轻便.界面炫.更新快,但是这样web的缺点也就出来了,就是不稳定,容易受用户等因素影响. 因为很多客户端web是内嵌的,内部通信,所以很多对安全的考 ...

  2. SQL注入测试用例

    //看看是什么权限的and 1=(Select IS_MEMBER('db_owner'))And char(124)%2BCast(IS_MEMBER('db_owner') as varchar( ...

  3. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

  4. SOA之(2)——SOA架构基础概念与设计框架

    SOA的设计框架 设计框架与架构相关的概念紧密相连,原则.模式和架构始终是与设计共舞的. SOA服务设计的原则中记录了一个基础的设计框架: 设计特性(Design Characteristic)——由 ...

  5. Ckeditor 中一些核心的对象的作用

    1.CKEditor CKEditor对象用于掌管全局,他是一个单例对象,管理着所有实例化了的编辑框. 通过replace方法创建编辑框实例. 2.CKEditor.editor 表示一个编辑框实例, ...

  6. UnityException: Texture is not readable

    原地址:http://blog.csdn.net/emoonight/article/details/18002913 fore you can save or load a Texture, you ...

  7. UVA 10635 Prince and Princess

    题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...

  8. procedure can't return a result set in the given context

    调用存储过程失败!出现如下错误:PROCEDURE ipbx.qu_ery can't return a result set in the given context, ipbx是数据库, qu_e ...

  9. Windows 代码实现关机(直接黑屏)

    整理资料的时候发现的以前的代码,本机Win7 x64 Sp1 运行直接关机,黑屏.就是利用RtlAdjustPrivilege函数提权,代码中的注释写的很详细了.用的VS2010写的,直接编译成x64 ...

  10. C语言一些常用内存分配函数

    首先看个问题程序(这里用的是TC编译器): #include "stdlib.h" #include "stdio.h" void main() {    in ...