A. Maze
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
3 4 2
#..#
..#.
#...
output
#.X#
X.#.
#...
input
5 4 5
#...
#.#.
.#..
...#
.#.#
output
#XXX
#X#.
X#..
...#
.#.#
题目大意:
找k个空白快'.'填充成'X',使剩下的空白快仍然组成联通块。
方法一:
dfs找到边缘块,从边缘块开始填充;如果你理解不了边缘块,可以看方法二。
代码1:
#include<iostream>
#include<cstdio>
using namespace std;
const int N=;
int n,m,k;
int dir[][]={
,,,,-,,,-
};
int vis[N][N]={ };
char map[N][N];
void dfs(int x,int y)
{
for(int i=;i<;i++)
{
int a=x+dir[i][];
int b=y+dir[i][];
if(<=a&&a<n&&<=b&&b<m&&vis[a][b]==&&map[a][b]=='.')
{
vis[a][b]=;
dfs(a,b);
}
}
if(k)
{
k--;
map[x][y]='X';
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
getchar();
for(int i=;i<n;i++)
{
gets(map[i]);
}
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
if(vis[i][j]==&&map[i][j]=='.')
{
vis[i][j]=;
dfs(i,j);
}
}
for(int i=;i<n;i++)
puts(map[i]);
return ;
}

方法二:

逆向思维,先把所有的空白块'.'变成'X',记录个数为ans,再用dfs把ans-k个'X'变成'.';这样就保证了所有的空白块都是连通块。

代码2:

#include<iostream>
#include<cstdio>
using namespace std;
const int N=;
int n,m,k;
int cnt=;
int ans=;
int dir[][]={
,,,,-,,,-
};
char map[N][N];
void dfs(int x,int y)
{
for(int i=;i<;i++)
{
int a=x+dir[i][];
int b=y+dir[i][];
if(<=a&&a<n&&<=b&&b<m&&map[a][b]=='X')
{
if(cnt==ans-k)return ;//控制'.'的个数为ans-k个
map[a][b]='.';
cnt++;
dfs(a,b);
}
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
getchar();
for(int i=;i<n;i++)
{
gets(map[i]);
}
int x=,y=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
if(map[i][j]=='.')
{
map[i][j]='X';
ans++;
x=i;
y=j;
}
}
map[x][y]='.';
cnt++;
dfs(x,y);
for(int i=;i<n;i++)
puts(map[i]);
return ;
}

Codeforces 377A - Maze的更多相关文章

  1. CodeForces - 377A Maze BFS逆思维

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

  2. codeforces 377A. Puzzles 水题

    A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...

  3. Codeforces 123E Maze(树形DP+期望)

    [题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...

  4. CodeForces - 123E Maze

    http://codeforces.com/problemset/problem/123/E 题目翻译:(翻译来自: http://www.cogs.pw/cogs/problem/problem.p ...

  5. CodeForces 378C Maze (DFS)

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

  6. Codeforces 404E: Maze 1D(二分)

    题意:指令“R”机器人会向右走一步,“L”是向左.起初机器人在0位置,可以在除了0以外的任何位置放障碍,如果机器人的指令将使它走到障碍上,那这一步他会保持不动.要求让机器人最终结束的那一步一定只走过一 ...

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

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

  8. Codeforces Round #222 (Div. 1) (ABCDE)

    377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...

  9. HZNU Training 4 for Zhejiang Provincial Collegiate Programming Contest 2019

    今日这场比赛我们准备的题比较全面,二分+数论+最短路+计算几何+dp+思维+签到题等.有较难的防AK题,也有简单的签到题.为大家准备了一份题解和AC代码. A - Meeting with Alien ...

随机推荐

  1. elasticsearch配置文件详解

    来自:http://www.searchtech.pro/articles/2013/02/18/1361194291548.html elasticsearch的config文件夹里面有两个配置文 ...

  2. Vue基础进阶 之 Vue生命周期与钩子函数

    Vue生命周期 Vue生命周期:Vue实例从创建到销毁的过程,称为Vue的生命周期: Vue生命周期示意图:https://cn.vuejs.org/v2/guide/instance.html#生命 ...

  3. Greenplum5.16.0 安装教程

    Greenplum5.16.0 安装教程 一.环境说明 1.1官方网站 Greenplum官方安装说明:https://gpdb.docs.pivotal.io/5160/install_guide/ ...

  4. Spring Boot(六):如何使用mybatis

    Spring Boot(六):如何使用mybatis orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动 ...

  5. nginx动静态分离以及配置https(安全组强行切换以及导致的问题解决)

    公司原来的网络采用http/https同时支持的方式,http并不会强制自动跳转到https,最近要求强制切换,导致了一系列问题.趁今天测试完成了,整理如下: 1.要求HTTP自动跳转到HTTPS: ...

  6. bzoj 4540 [HNOI 2016] 序列 - 莫队算法 - Sparse-Table - 单调栈

    题目传送门 传送点I 传送点II 题目大意 给定一个长度为$n$的序列.询问区间$[l, r]$的所有不同的子序列的最小值的和. 这里的子序列是连续的.两个子序列不同当且仅当它们的左端点或右端点不同. ...

  7. 网络 --- 4 socketserver模块并发 连接合法性

    一.socketserver模块 ②服务端 ③客户端 二.连接合法性       ①os.urandom(n) 一种bytes类型的随机生成n个字节字符串的方法 而且每次生成的值都不相同.再加上md5 ...

  8. Manjaro kde 18.0安装与基本配置

    目录 更换源镜像.更新系统 安装搜狗输入法 安装软件 系统配置 最后清理垃圾 首先用Rufus制作启动U盘安装,设置好时间和日期等 更换源镜像.更新系统 排列源(只选清华源mirrors.tuna.t ...

  9. 【做题】uoj#370滑稽树上滑稽果——巧妙dp

    一个显然的结论是最终树的形态必然是一条链.具体证明只要考虑选定树上的某一条链,然后把其他部分全部接在它后面,这样答案一定不会变劣. 那么,一开始的想法是考虑每一位的最后出现位置,但这并不容易实现.注意 ...

  10. spring boot + session+redis解决session共享问题

    自己没有亲自试过,不过看了下这个例子感觉靠谱,以后做了测试,在加以说明. PS:后期经验证,上面例子可行.我们平时存session里面的值,直接存在了redis里面了.