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跟着原路返回时插入就行了。
====================================================================================================================================================
代码:
 #include<bits/stdc++.h>
using namespace std;
char Map[][];
bool book[][];
int n,m,k;
void dfs(int x,int y)
{
if(x<||x>=n||y<||y>=m) return;
if(Map[x][y]!='.'||book[x][y]==) return ;
book[x][y]=;
for(int i=;i<=;i++)
{ //四个方向
if(i==) dfs(x+,y);
if(i==) dfs(x-,y);
if(i==) dfs(x,y+);
if(i==) dfs(x,y-);
}
//当遍历到底了之后,即循环了四次无法继续走下去时
if(k!=)
Map[x][y]='X',k--;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<n;++i)
scanf("%s",Map[i]); for(int i=;i<n;++i)
{
for(int j=;j<m;j++)
{
dfs(i,j);
if(k==) break;
}
if(k==) break;
}
for(int i=;i<n;i++)
puts(Map[i]);
return ;
}

17-比赛2 C - Maze (dfs)的更多相关文章

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

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

  2. Maze dfs倒行

    Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or i ...

  3. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

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

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

  5. CodeForces 378C Maze (DFS)

    题目链接 题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案. 分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘ 一直填充k个 ...

  6. (比赛)B - 棋盘问题(dfs)

    B - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Practice POJ ...

  7. Magic Maze dfs + dp

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

  8. 图的基本算法(BFS和DFS)(转载)

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

  9. BFS和DFS算法

    昨晚刚昨晚华为笔试题,用到了BFS和DFS,可惜自己学艺不精,忘记了实现原理,现在借用大佬写的内容给自己做个提高 转自:https://www.jianshu.com/p/70952b51f0c8 图 ...

  10. BFS(广搜)DFS(深搜)算法解析

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

随机推荐

  1. angular解决压缩问题,和传送数据

    1.angular解决压缩的方法 var app = angular.module("mk",[]); app.controller("ctrl",['$sco ...

  2. (13)JavaScript之[HTML DOM元素][JS对象]

    元素 /** * HTML DOM 元素(节点)*/ //创建新的HTML元素 var para = document.createElement('p'); var node = document. ...

  3. Android Studio 创建虚拟机失败 Failed to load 解决办法

    Name: Nexus_S_API_21 CPU/ABI: ARM (armeabi) Path: H:\Users\Pavkoo\.android\avd\Nexus_S_API_21.avd Er ...

  4. ansible使用4-Playbook Roles and Include Statements

    task include --- # possibly saved as tasks/foo.yml - name: placeholder foo command: /bin/foo - name: ...

  5. java compiler没有1.8怎么办

    选择第一个点击安装,安装完成后,重启eclipse,打开java compiler 就可以选择1.8了. 成功:  扫个红包吧! Donate捐赠 如果我的文章帮助了你,可以赞赏我 1 元,让我继续写 ...

  6. 对json数据进行排序

    项目有这样一个需要对数据库取出的数据按sort字段进行显示:这个时候想起来在JS中对json数据进行一个排序再进行数据填充可以实现此效果 var colId = "sort";// ...

  7. Maven报错:Missing artifact jdk.tools:jdk.tools:jar:1.6

    1.jdk.tools:jdk.tools是与JDK一起分发的一个JAR文件,可以如下方式加入到Maven项目中: <dependency>    <groupId>jdk.t ...

  8. MYSQL:随机抽取一条数据库记录

    今天我们要实现从随机抽取一条数据库记录的功能,并且抽取出来的数据记录不能重复: 1.首先我们看文章表中的数据: 2.实现功能代码如下: 1 /** * 获取随机的N篇文篇 * @param int $ ...

  9. *5. Longest Palindromic Substring (dp) previous blogs are helpful

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  10. 【UOJ83】【UR #7】水题出题人(提交答案题)

    点此看题面 大致题意: 给你若干份排序的代码,共\(6\)个子任务,每个子任务让你构造数据使得一份代码用时在给定的\(T\)以内,另一份代码用时超过\(2000000\). 子任务\(1\):归并排序 ...