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. Linux Centos 7.4 内核升级

    Linux Centos 7.4 内核升级 原始内核版本:3.10.0-693.2.2.el7.x86_64 升级内核版本:4.14.9-1.el7.elrepo.x86_64 1.导入key Key ...

  2. Examples of Scikit-learn Usages

    Examples of Scikit-learn Usages KFold K-折交叉验证 >>> import numpy as np >>> from skle ...

  3. python简说(十)json模块

    常用模块: 一个python文件就是一个模块 1.标准模块,python自带的 2.第三方模块,需要安装 3.自己写的python文件 json,就是一个字符串 1.json转为字典 json_str ...

  4. IIC学习

    1 概述: IIC是用两条双向的线,一条SDA(serial data line),一条SCL(serial clock). SCL:上升沿将数据输入到每个EEPROM器件中,下降沿驱动EEPROM器 ...

  5. 【python016--序列】

    一.列表,元组和字符串的共同点 --都可以通过索引得到每一个元素 --默认索引值总是从0开始 --可以通过分片的方法得到一个范围内的元素的集合 --有很多共同的操作符(重复操作符,拼接操作符.成员关系 ...

  6. 思维|蚂蚁感冒|2014年蓝桥杯A组题解析第七题-fishers

    标题:蚂蚁感冒 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁中,有 ...

  7. 学习使用JUnit4进行单元测试

    借用http://blog.csdn.net/andycpp/article/details/1327147等文章上面的例子和教程进行学习总结,自己敲了一遍代码,发现里面有些东西,可能版本原因,已经稍 ...

  8. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  9. docker的安装和使用

    docker在linux上安装我尝试了几次,都报错了,看到其它人的博客说明这也确实是个问题. 后来在朋友给安装了一个VMware虚拟机后,很方便地就在里面进行了安装. 概念理解: 仓库: 别人做好的现 ...

  10. Kubernetes之容器

    Images You create your Docker image and push it to a registry before referring to it in a Kubernetes ...