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 ;
}

CodeForces - 377A Maze BFS逆思维的更多相关文章

  1. Codeforces 377A - Maze

    A. Maze 题目链接:http://codeforces.com/contest/377/problem/A time limit per test 2 seconds memory limit ...

  2. [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)

    题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...

  3. CodeForces - 987E Petr and Permutations (思维+逆序对)

    题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...

  4. Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  5. CodeForces 1292A NEKO's Maze Game(思维)

    #include <stdio.h> #include <string.h> #include <iostream> #include <string> ...

  6. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  7. Codeforces Gym 100463A Crossings 逆序数

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  8. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  9. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

随机推荐

  1. duplicate symbols for architeture arm64 linker command failed with code 1(use-c to see invocation)

    duplicate symbols for architeture arm64  linker command failed with code 1(use-c to see invocation) ...

  2. it starts (“forks”) a new process for each connection.

    PostgreSQL: Documentation: 10: 1.2. Architectural Fundamentals https://www.postgresql.org/docs/10/st ...

  3. `npm install`卡住不动,使用`sudo npm install`就可以下载依赖包

    当我在项目中执行npm install的时候,等了几分钟也没有打印信息出来,竟然卡住不动了. 我取消之后再执行sudo npm install发现是可以安装的.只是安装的node_models文件夹不 ...

  4. window.onerror 错误监听,发到后台

    var doc = document.body || document.documentElement; var _onerror = Onerror(''); var Onerror = funct ...

  5. appium():PageObject&PageFactory

    Appium Java client has facilities which components to Page Object design pattern and Selenium PageFa ...

  6. PAT天梯赛 L2-026. 小字辈 【BFS】

    题目链接 https://www.patest.cn/contests/gplt/L2-026 思路 用一个二维vector 来保存 每个人的子女 然后用BFS 广搜下去,当目前的状态 是搜完的时候 ...

  7. Gym - 100676F Palindrome —— 并查集

    题目链接:https://vjudge.net/contest/155789#problem/E 题解: 由于是回文串,所以可以先将在对称位置的字符放在同一个集合(如果期间有两个非‘?’,且不相等,则 ...

  8. Redis缓存服务搭建及实现数据读写 - Eric.Chen

    发现博客园中好多大牛在介绍自己的开源项目是很少用到缓存,比如Memcached.Redis.mongodb等,今天得空抽时间把Redis缓存研究了一下,写下来总结一下,跟大家一起分享 一下.由于小弟水 ...

  9. 基于BASYS2的VHDL程序——数字钟(最终版)

    转载请注明原地址:http://www.cnblogs.com/connorzx/p/3674178.html 调时电路正常工作.一切正常.发现做FPGA还是得从数电的思路思考,设置一个预置使能端,预 ...

  10. java编程思想-基础

    interface: 方法默认为public:成员变量默认 static and final 对象数组的定义:理解? 多接口继承:可以多个接口,但只有一个具体类,具体类在前面 自:多接口继承时,来自不 ...