Codeforces 377A - Maze
2 seconds
256 megabytes
standard input
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.
The first line contains three integers n, m, k (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.
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.
3 4 2
#..#
..#.
#...
#.X#
X.#.
#...
5 4 5
#...
#.#.
.#..
...#
.#.#
#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的更多相关文章
- CodeForces - 377A Maze BFS逆思维
Maze Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- Codeforces 123E Maze(树形DP+期望)
[题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...
- CodeForces - 123E Maze
http://codeforces.com/problemset/problem/123/E 题目翻译:(翻译来自: http://www.cogs.pw/cogs/problem/problem.p ...
- CodeForces 378C Maze (DFS)
题目链接 题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案. 分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘ 一直填充k个 ...
- Codeforces 404E: Maze 1D(二分)
题意:指令“R”机器人会向右走一步,“L”是向左.起初机器人在0位置,可以在除了0以外的任何位置放障碍,如果机器人的指令将使它走到障碍上,那这一步他会保持不动.要求让机器人最终结束的那一步一定只走过一 ...
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- Codeforces Round #222 (Div. 1) (ABCDE)
377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...
- HZNU Training 4 for Zhejiang Provincial Collegiate Programming Contest 2019
今日这场比赛我们准备的题比较全面,二分+数论+最短路+计算几何+dp+思维+签到题等.有较难的防AK题,也有简单的签到题.为大家准备了一份题解和AC代码. A - Meeting with Alien ...
随机推荐
- 【题解】4465 [Jsoi2013]游戏中的学问
原题传送门 线性dp推一推就推出方程 设\(f[i][j]\)表示有\(j\)个人,分成\(i\)组,一共有多少分发 边界为\(f[0][0]=1\),珂以得出方程为\(f[i][j]=(j-1)*( ...
- Linux基础笔记——RAID
关于RAID RAID0:理论上来说一个有n块磁盘组成的raid0,它的读写是单个磁盘性能的n倍,具有低成本,高性能,低安全性,可用于可靠性不高的应用,如:视频.音频.临时数据存储等 RAID1:也称 ...
- Firemonkey的几个特色属性(一)
基于FireMonkey的程序开发与VCL确实有些不同,很多属性发生了变化,尤其是外观方面. 1.Margins.Padding.Position Margins:指定了当前控件在父控件(Parent ...
- 在VMware14上安装centos6.5
打开vmware14 => 创建新虚拟机(即再建一个linux),已有光盘映像文件,正常操作即可.
- IDEA 入门
IDEA初步使用 IntelliJ IDEA 使用教程(2019图文版) -- 从入门到上瘾 IntelliJ IDEA 设置代码提示或自动补全的快捷键 (Alt+/) IntelliJ IDEA 配 ...
- Cannot retrieve metalink for repository: epel 错误解决办法
centos下安装完EPEL源, 然后更新一下yum缓存, 如果发现这样的错误:Error: Cannot retrieve metalink for repository: epel. Please ...
- SQL 多行合并一行
select stuff((select ',' + CONVERT(VARCHAR(50),id)+'' from tab_menu group by id for xml path('')), ...
- 【Hadoop 分布式部署 十 一: NameNode HA 自动故障转移】
问题描述: 上一篇就是NameNode 的HA 部署完成,但是存在问题,问题是如果 主NameNode的节点宕机了,还是需要人工去使用命令来切换NameNode的Acitve 这样很不方便,所以 ...
- Bytom交易说明(UTXO用户自己管理模式)
比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 该部分主 ...
- 以结算价交易TAS和以市价交易TAM
CME Group的合约规格中提到TAS和TAM交易,如:Gold Futures Contract Specs Gold 期货 合约规格 Trading at Settlement (TAS) is ...