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 nmk (1 ≤ n, m ≤ 500, 0 ≤ k < s), where nand 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.

Examples

Input
3 4 2#..#..#.#...

Output
#.X#X.#.#...

Input
5 4 5#...#.#..#.....#.#.#

Output
#XXX#X#.X#.....#.#.#

题意: 插入 k 个 X 且保持所有的 '.' 保持贯通====================================================================================================================================================怎么样才能使插入的 X 没有阻断路的连通,,想一想DFS,不撞南墙不回头的理论,只要沿着一个点一路走到底,直到不能走为止,那么这个末端放上X一定不会阻断其它'.'的连贯如果所有末端 插入完毕之后,还有X没有放入,那么末端之后紧挨着的点就变成了末端,根据DFS走到末端后会原路返回,那么剩下的X跟着原路返回时插入就行了。
====================================================================================================================================================代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char Map[510][510];
 4 bool book[510][510];
 5 int n,m,k;
 6 void dfs(int x,int y)
 7 {
 8     if(x<0||x>=n||y<0||y>=m) return;
 9     if(Map[x][y]!='.'||book[x][y]==1) return ;
10     book[x][y]=1;
11     for(int i=1;i<=4;i++)
12     {   //四个方向
13         if(i==1)  dfs(x+1,y);
14         if(i==2)  dfs(x-1,y);
15         if(i==3)  dfs(x,y+1);
16         if(i==4)  dfs(x,y-1);
17     }
18     //当遍历到底了之后,即循环了四次无法继续走下去时
19     if(k!=0)
20         Map[x][y]='X',k--;
21 }
22 int main()
23 {
24       scanf("%d%d%d",&n,&m,&k);
25         for(int i=0;i<n;++i)
26             scanf("%s",Map[i]);
27
28         for(int i=0;i<n;++i)
29             {
30                 for(int j=0;j<m;j++)
31                 {
32                     dfs(i,j);
33                     if(k==0) break;
34                 }
35                 if(k==0) break;
36             }
37         for(int i=0;i<n;i++)
38             puts(Map[i]);
39     return 0;
40 }

转载于:https://www.cnblogs.com/darkboy/p/9415934.html

17-比赛2 C - Maze (dfs)相关推荐

  1. Magic Maze dfs + dp

    http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...

  2. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  3. 【HDU - 1045】Fire Net (dfs 或二分图)

    题干: Suppose that we have a square city with straight streets. A map of a city is a square board with ...

  4. LeetCode 490. 迷宫(BFS/DFS)

    文章目录 1. 题目 2. 解题 2.1 BFS 2.2 DFS 1. 题目 由空地和墙组成的迷宫中有一个球. 球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动. 当球停下时,可以选择下一个方 ...

  5. DFS(深搜)算法解析

    图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...

  6. hdu1198 Farm Irrigation —— dfs or 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: 1 #include<cstdio>//hdu1198 dfs 2 #in ...

  7. 【算法】蓝桥杯dfs深度优先搜索之排列组合总结

    [导航] 上一篇文章 → <[算法]蓝桥杯dfs深度优先搜索之凑算式总结>   为了重申感谢之意,再次声明下文的大部分灵感均来自于[CSDN]梅森上校<JAVA版本:DFS算法题解两 ...

  8. 【7.17总结】 匈牙利算法(二分图最大匹配)

    二分图就是把图的点集分成X和Y两个集合且连接每条边的两个顶点分别在两个集合. 匹配是指图的边集的一个子集,其中任意两条边没有相同的顶点. 最大匹配就是找到一个匹配使得匹配里面的边数最多. 增广路径就是 ...

  9. 17AHU排位赛3 D题 旋转吧!雪月花 ! (DFS序,线段树维护树上最值)

    problem 在一个平面上有n个齿轮,每个齿轮都有自己的初始半径 ri r_i .有n-1对齿轮是互相嵌在一起的,即它们拥有相同的线速度.如果将n个齿轮当作n个点,将n-1条相嵌关系当作n-1条边, ...

  10. 比赛经验总结--187

    cqround1 1.这次比赛160分 (1)反质数:60 (2)二分答案+2sat:验证:  0分 (3)树上LCA+推公式: 100分 2.总结:这次比赛第三题做起来非常顺,做过类似的题:奶牛大集 ...

最新文章

  1. unity, TRANSFORM_TEX
  2. python的tell和seek_python文件对象的seek和tell
  3. 第三次学JAVA再学不好就吃翔(part66)--Pattern类和Matcher类
  4. Ubuntu如何安装setuptools
  5. jQuery 的选择器 元素选择器
  6. 【转载】一次项目管理交流会总结
  7. mysql path密码忘记_mysql密码忘记
  8. linux 下创建GRE隧道
  9. 小程序飞入购物车特效
  10. Ros学习——launch文件解析
  11. 【MTSP】基于matlab遗传算法求解多旅行商问题【含Matlab源码 1325期】
  12. c语言删除堆栈所有的结点,深入浅出数据结构C语言版(15)——优先队列(堆)(示例代码)...
  13. mpvue 用户授权登录
  14. NLP ---文本情感分析
  15. 关于aab转apk的方法--bundletool
  16. 密码破解---实验八:Windows本地破解用户口令
  17. 支付业务与技术架构学习总结(1)——完整的支付系统整体架构
  18. 互联网产品经理必读书目推荐
  19. python dataframe合并相同行_pandas之DataFrame合并merge
  20. MySQL5.7修改密码问题解决方案

热门文章

  1. 泛型类有什么作用_3 分钟带你彻底搞懂 Java 泛型背后的秘密
  2. appium for java教程_appium自动化测试入门(java版)
  3. Vue3 slot插槽——(默认插槽、具名插槽、作用域插槽)
  4. Nodejs 新特性 async await 的使用 以及使用 async await 处理异步
  5. LeetCode 2109. 向字符串添加空格
  6. LeetCode 1739. 放置盒子(数学)
  7. LeetCode 1781. 所有子字符串美丽值之和
  8. LeetCode 1670. 设计前中后队列(deque)
  9. LeetCode 911. 在线选举(二分查找)
  10. LeetCode 1243. 数组变换