Maze

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 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#..
...#
.#.# 这是一道好题!题意是通过添加k个障碍使得原图继续保持连通状态。当然不能搜索加点,这会破坏之前的连通性。因此我们可以搜索出一个连通块,使得大小=kk-k(kk为原图可行域.点数) 没被标记的点除#墙外,未搜索的可行域.就是加点X位置。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; int n,m,k,kk;
char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}};
struct Node{
int x,y;
}node; void bfs(int i,int j)
{
int c,tx,ty,ij;
queue<Node> q;
memset(b,,sizeof(b));
b[i][j]=;
node.x=i;
node.y=j;
q.push(node);
c=;
if(c==kk-k) return; //卡在第8个点,不加TLE。。
while(q.size()){
for(ij=;ij<;ij++){
tx=q.front().x+t[ij][];
ty=q.front().y+t[ij][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='.'&&b[tx][ty]==){
c++;
b[tx][ty]=;
node.x=tx;
node.y=ty;
q.push(node);
}
if(c==kk-k) return;
}
q.pop();
}
}
int main()
{
int bx,by,i,j;
scanf("%d%d%d",&n,&m,&k);
kk=;
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='.'){
kk++;
bx=i;
by=j;
}
}
}
bfs(bx,by);
for(i=;i<n;i++){
for(j=;j<m;j++){
if(b[i][j]==&&a[i][j]!='#') printf("X");
else printf("%c",a[i][j]);
}
printf("\n");
}
return ;
}

最新文章

  1. install google chrome
  2. hdu 1520 Anniversary party 基础树dp
  3. hdu 5542 The Battle of Chibi(2015CCPC - C题)
  4. java实例练习
  5. 山东省第六届ACM省赛
  6. Linux下yum安装MPlayer 或 LVC视频播放器
  7. oracle创建自增长列
  8. 前台页面Josn 数组在后台.cs代码中的解析
  9. PHP glob() 函数
  10. c++ primer复习(三)
  11. 3-07. 求前缀表达式的值(25) (ZJU_PAT数学)
  12. Chorme 快捷键
  13. python爬虫从入门到放弃(六)之 BeautifulSoup库的使用
  14. SQL练习题完整(做完你就是高手)
  15. 【Beta阶段】测试与发布
  16. 【Python】 用户图形界面GUI wxpython I 基本用法和组件
  17. 10 种保护 Spring Boot 应用的绝佳方法
  18. Fiddler 学习笔记---命令、断点
  19. linphone
  20. HDU 6073 Matching In Multiplication(拓扑排序+思维)

热门文章

  1. Web性能测试工具:Siege安装&amp;使用简介
  2. hbase shell删除没实用
  3. C# WPF DataGrid控件实现三级联动
  4. IOS下SQLite的简单使用
  5. EL表达式 介绍
  6. 【Effective C++】让自己习惯C++
  7. AndroidPageObjectTest_Chained.java
  8. oracle rac常用的网络检查命令
  9. JavaScript中浮点数的保留小数位数的问题
  10. Discuz/X3.1去掉标题中的Powered by Discuz!以及解决首页标题后的&quot;-&quot;