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. Typekit在线字库及使用方法

    一.如果设计中使用了非标准的字体,你该如何去实现? 所谓的标准字体是多数机器上都会有的,或者即使没有也可以由默认字体替代的字体. 方法: 用图片代替 web fonts在线字库,如Google Web ...

  2. 小行星碰撞 Asteroid Collision

    2018-08-07 11:12:01 问题描述: 问题求解: 使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行. 遍历整个数组,对于每个读到的 ...

  3. Enduring Exodus CodeForces - 655C (二分)

    链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...

  4. Up and Down the Tree CodeForces - 1065F (树形dp)

    链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...

  5. Leetcode 79

    //这是我写过最难的递归了...//class Solution { public: bool exist(vector<vector<char>>& board, s ...

  6. ps -ef |grep xxx 输出的具体含义

    ps:将某个进程显示出来 -A 显示所有程序. -e 此参数的效果和指定"A"参数相同. -f 显示UID,PPIP,C与STIME栏位. grep命令是查找 中间的|是管道命令 ...

  7. 网络SSID是什么意思

    ssid是网络的ID(名称).一般用在无线网络上.搜索无线网络名一般就是在搜索无线网络的ssid. SSID是Service Set Identifier的缩写,意思是:服务集标识.SSID技术可以将 ...

  8. 4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. Oracle ASM 相关的 视图(V$) 和 数据字典(X$)

    本文转自: http://blog.csdn.net/tianlesoftware/article/details/6733039 ASM由于其高度的封装性,使得我们很难知道窥探其内部的原理.可以通过 ...

  10. SQL中游标的使用(转)

    http://www.cnblogs.com/tianguook/archive/2011/03/09/1977987.html 一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言, ...