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. What is probabilistic programming? | 中文翻译

    What is probabilistic programming? | 中文翻译 Probabilistic languages can free developers from the compl ...

  2. 关于我在17号“一个查询任意年份中任意月份的天数”程序编写中的代码&第二种方法!

    PS:下面的代码是我对于17号的练习题的一些新的看法(其实就是从另一个角度思考问题) package day20180917;import java.util.Scanner;//导包public c ...

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

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

  4. 学习Struts2的个人疑惑及问题解决

    刚开始学习SSH框架中Struts2时,个人疑惑以及一些问题总结一下. 1.package节点namespace属性值决定访问路径问题       namespace不写或写namespace=&qu ...

  5. linux内核发生Oops时怎么办?

    1. 定位发生Oops的代码 1.1 通过addr2line命令定位 aarch64-openwrt-linux-gnu-addr2line -e vmlinux ffff000008087f00 1 ...

  6. 密码发生器|2012年蓝桥杯B组题解析第八题-fishers

    (10')密码发生器 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如果设置不好记的密码,又担心自己也会忘记:如果写在纸上,担心纸张被别人发现 ...

  7. 【调优】kafka性能调优

    主要优化原理和思路 kafka是一个高吞吐量分布式消息系统,并且提供了持久化.其高性能的有两个重要特点: 利用了磁盘连续读写性能远远高于随机读写的特点: 并发,将一个topic拆分多个partitio ...

  8. R语言 union、setdiff、insect

    union 求两个向量的并集集合可以是任何数值类型 union(x=1:3, y=2:5)[1] 1 2 3 4 5 union(x=c("abc", "12" ...

  9. Chrome上的扩展工具

    rest client - rest api Testing:类似fiddler的工具,可以测试url,查看返回值……

  10. 授权oAuth

    使用Client Credentials Grant授权方式给客户端发放access token 只验证客户端(Client),不验证用户(Resource Owner),只要客户端通过验证就发acc ...