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.

Example

Input
3 4 2
#..#
..#.
#...
Output
#.X#
X.#.
#...
Input
5 4 5
#...
#.#.
.#..
...#
.#.#
Output
#XXX
#X#.
X#..
...#
.#.# 一开始用的广搜,不对,广搜没啥合理性,实在是找不出道理,但是深搜可以,深搜可以把所有点连成一棵树,按顺序删除一些结点不会影响连通性,所以用递归
dfs直到四个方位都遍历过了,再把这个结点删除。 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
int n,m,k,rc=,sx,sy;
int dir[][]={,,,,,-,-,};
char mp[][];
int vis[][];
void dfs(int x,int y)
{
if(x<||y<||x>=n||y>=m)return;
if(mp[x][y]=='#'||vis[x][y])return ;
vis[x][y]=;
for(int i=;i<;i++)
{
int tx=x+dir[i][];
int ty=y+dir[i][];
dfs(tx,ty);
}
if(k)mp[x][y]='X',k--;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>m>>k;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='.')sx=i,sy=j;
else vis[i][j]=;
}
} dfs(sx,sy);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
putchar(mp[i][j]); putchar('\n');
}
}

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. 基于上三角变换或基于DFS的行(列)展开的n阶行列式求值算法分析及性能评估

    进入大一新学期,看完<线性代数>前几节后,笔者有了用计算机实现行列式运算的想法.这样做的目的,一是巩固自己对相关概念的理解,二是通过独立设计算法练手,三是希望通过图表直观地展现涉及的两种算 ...

  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. POJ 3083 BFS+DFS 40行

    题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...

  6. CodeForces 378C Maze (DFS)

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

  7. Magic Maze dfs + dp

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

  8. 选课 树形DP+多叉树转二叉树+dfs求解答案

    问题 A: 选课 时间限制: 1 Sec  内存限制: 128 MB 题目描述 大 学里实行学分.每门课程都有一定的学分,学生只要选修了这门课并考核通过就能获得相应的学分.学生最后的学分是他选修的各门 ...

  9. poj3764(dfs+Trie树+贪心)

    题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...

随机推荐

  1. 【转】cs231n学习笔记-CNN-目标检测、定位、分割

    原文链接:http://blog.csdn.net/myarrow/article/details/51878004 1. 基本概念 1)CNN:Convolutional Neural Networ ...

  2. sublime插件安装及常用插件配置

    1.下载 :百度云 工具中 2.注册 sgbteam Single User License EA7E-1153259 8891CBB9 F1513E4F 1A3405C1 A865D53F 115F ...

  3. spoj Help the Military Recruitment Office!

    题意:给出名字和地方,地方会重定向,最后再给出名字,问现在属于哪里? 用并查集. //#pragma comment(linker,"/STACK:1024000000,1024000000 ...

  4. 『Sklearn』特征向量化处理

    『Kaggle』分类任务_决策树&集成模型&DataFrame向量化操作 1 2 3 4 5 6 7 8 9 '''特征提取器''' from sklearn.feature_extr ...

  5. POJ-3087 Shuffle'm Up (模拟)

    Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...

  6. Session的方法getSession() 与 getSession(boolean para)区别

    getSession(boolean para)返回当前http会话,如果不存在,则创建一个新的会话getSession() 调用getSession(true)的简化版 [官方解释] getSess ...

  7. Mac安装fish shell

    1.brew update 2.brew install fish 3.sudo vi /etc/shells 增加内容:/usr/local/bin/fish   ##增加fish到shell环境变 ...

  8. Centos7上部署openstack ocata配置详解

    之前写过一篇<openstack mitaka 配置详解>然而最近使用发现阿里不再提供m版本的源,所以最近又开始学习ocata版本,并进行总结,写下如下文档 OpenStack ocata ...

  9. @SpringBootApplication的使用

    之前用户使用的是3个注解注解他们的main类.分别是@Configuration,@EnableAutoConfiguration,@ComponentScan.由于这些注解一般都是一起使用,spri ...

  10. VS2010创建动态链接库(DLL)的方法

    1.第一步创建WIN32项目,选择DLL 2.第二步,创建你自己的DLL CPP文件和头文件,下面以两个简单的加减法函数为例子导出 然后编译生成即可.DLL文件在Debug或Release目录中 .d ...