A. Maze

题目连接:

http://codeforces.com/contest/377/problem/A

Description

Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.

Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.

Input

The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze.

Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.

Output

Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").

It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.

Sample Input

3 4 2

..#

..#.

...

Sample Output

.X#

X.#.

...

Hint

题意

在一个nm的矩阵里面,你需要画k个'X'使得,剩下的.都在一个连通块里面

题解:

我们这么想,我们只要按照dfs的顺序去涂X就好了

如果一开始就有多个连通块的话,我们最后剩下的是最大的连通块,其他的一定都可以被填满的

然后再dfs去填就好了

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int x,y,z,k;
  5. };
  6. const int maxn = 505;
  7. int n,m,k;
  8. int dx[4]={1,-1,0,0};
  9. int dy[4]={0,0,1,-1};
  10. char mp[maxn][maxn];
  11. int vis[maxn][maxn];
  12. int cnt=1,sum=0;
  13. vector<node>P;
  14. bool cmp(node a,node b)
  15. {
  16. return a.k>b.k;
  17. }
  18. void dfs(int x,int y)
  19. {
  20. sum++;
  21. vis[x][y]=cnt;
  22. for(int i=0;i<4;i++)
  23. {
  24. int xx = x+dx[i];
  25. int yy = y+dy[i];
  26. if(xx<1||xx>n)continue;
  27. if(yy<1||yy>m)continue;
  28. if(vis[xx][yy])continue;
  29. if(mp[xx][yy]=='#')continue;
  30. dfs(xx,yy);
  31. }
  32. }
  33. void dfs2(int x,int y)
  34. {
  35. vis[x][y]=1;
  36. for(int i=0;i<4;i++)
  37. {
  38. int xx = x+dx[i];
  39. int yy = y+dy[i];
  40. if(xx<1||xx>n)continue;
  41. if(yy<1||yy>m)continue;
  42. if(vis[xx][yy])continue;
  43. if(mp[xx][yy]=='#')continue;
  44. dfs2(xx,yy);
  45. }
  46. if(k>0){
  47. mp[x][y]='X';
  48. k--;
  49. }
  50. }
  51. int main()
  52. {
  53. scanf("%d%d%d",&n,&m,&k);
  54. for(int i=1;i<=n;i++)
  55. scanf("%s",mp[i]+1);
  56. node tmp;
  57. for(int i=1;i<=n;i++)
  58. {
  59. for(int j=1;j<=m;j++)
  60. {
  61. if(mp[i][j]=='.'&&vis[i][j]==0)
  62. {
  63. sum=0;
  64. dfs(i,j);
  65. tmp.x=i,tmp.y=j,tmp.z=cnt,tmp.k=sum;
  66. cnt++;
  67. P.push_back(tmp);
  68. }
  69. }
  70. }
  71. if(cnt==1)
  72. {
  73. for(int i=1;i<=n;i++,cout<<endl)
  74. for(int j=1;j<=m;j++)
  75. cout<<mp[i][j];
  76. return 0;
  77. }
  78. sort(P.begin(),P.end(),cmp);
  79. int ans1 = P[0].z,ans2 = k;
  80. for(int i=1;i<=n;i++)
  81. {
  82. for(int j=1;j<=m;j++)
  83. {
  84. if(mp[i][j]=='#')continue;
  85. if(vis[i][j]!=ans1)
  86. {
  87. mp[i][j]='X';
  88. k--;
  89. }
  90. }
  91. }
  92. memset(vis,0,sizeof(vis));
  93. dfs2(P[0].x,P[0].y);
  94. for(int i=1;i<=n;i++,cout<<endl)
  95. {
  96. for(int j=1;j<=m;j++)
  97. {
  98. cout<<mp[i][j];
  99. }
  100. }
  101. }

Codeforces Round #222 (Div. 1) A. Maze dfs的更多相关文章

  1. Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)

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

  2. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  3. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  4. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

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

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

  6. Codeforces Round #321 (Div. 2)C(tree dfs)

    题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...

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

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

  8. Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

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

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

随机推荐

  1. 好用的工具---screen命令

    问 题场景:要在服务器上配置环境,但是我的电脑无法直接连到服务器上,通常要经过好几次ssh跳转.配环境需要设置好几个用户,这自然需要同时打开好几个连 接服务器的终端窗口,每个连接到服务器的终端窗口都要 ...

  2. docker stack 部署 filebeat

    =============================================== 2018/7/21_第3次修改                       ccb_warlock 更新 ...

  3. 纯css进度条,各种兼容

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD&g ...

  4. 【Android开发日记】之入门篇(七)——Android数据存储(上)

    在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...

  5. python基础学习之路No.2 数据类型

    python中常见的数据类型有:整数.浮点数.字符串.列表.元组.字典 python相较其他语言,可以省略了声明,可以直接定义赋值使用. 例如: a=12 就相当于 其他语言中的  int a=12  ...

  6. Javascript之对象的创建

    面向对象语言有一个非常显著的标志,那就是它们都有类的概念,通过类之间的继承就可以达到任意创建具有相同属性方法的对象.而在ECMAScript中并没有类的概念,它把对象定义为:无序属性的集合,其属性包含 ...

  7. matlab转python

    最近在做把matlab代码转成python代码,没有用过matlab,python也只是局限于爬虫,所以.... matlab与python最大的不同是,matlab的下标是从1开始的,python和 ...

  8. I​n​n​o​ ​s​e​t​u​p​ ​常​用​修​改​技​巧

    Inno setup 常用修改技巧1 .如何让协议许可页面默认选中我同意按钮 [code]procedure InitializeWizard();beginWizardForm.LICENSEACC ...

  9. python学习day4之路

    装饰器(http://egon09.blog.51cto.com/9161406/1836763) 1.装饰器:本质是函数: 装饰器(装饰其他函数),就是为其他函数添加附加功能: 原则:1.不能修改被 ...

  10. Rookey.Frame之数据库及缓存配置

    上一篇中讨论了Rookey.Frame框架菜单配置功能,这一节我们继续学习Rookey.Frame框架的数据库连接配置. 之前介绍了Rookey.Frame框架支持跨多数据库,并且支持读写分离,不过目 ...