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 ...
随机推荐
- oracle 11g中的自动维护任务管理
因为人员紧缺,最近又忙着去搞性能优化的事情,有时候真的是不想再搞这个事情,只是没办法,我当前的绩效几乎取决于这个项目的最终成绩,所以不管是人的事还是事的事,都得去让他顺利推进. 前段时间发生还有几台服 ...
- 实现 AD 采样,使用 LCD1602 显示 AD 数值
实现 AD 采样,使用 LCD1602 显示 AD 数值 写在前面 单片机内集成的A/D转换,一般都有相应的特殊功能寄存器来设置A/D的使能标志,参考电压,转换频率,通道选择,A/D输入口的属性(模拟 ...
- host文件的作用
什么是host文件 Hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Host ...
- python --- 07 补充( join 删除和添加 fromkeys ) 深浅拷贝
一.基本数据类型补充 1.join() "*".join("马虎疼") # 马*虎*疼 把传递进去的参数进行迭代. 获取到的每个元素和前面的*进行拼接. 得到 ...
- topcoder srm 681 div1
problem1 link 二分答案.然后判断.将所有的机器按照$a_{i}$排序,$a_{i}$相同的按照$b_{i}$排序.用一个优先队列维护这些机器.这样对于第$i$个部分,拿出队列开始的机器来 ...
- Python3基础 list for+continue 输出1-50之间的偶数
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- C Primer Plus 创建友好的输入界面 笔记
看代码 char inputFunCode; while(inputFunCode = getchar()){ '){ printf("you choose string conn\n&qu ...
- Elasticsearch查询Index以及删除
查询Index信息 GET /bank HTTP/1.1Host: localhost:9200 { "bank": { "aliases": {}, &quo ...
- 使用Java Api 操作HDFS
如题 我就是一个标题党 就是使用JavaApi操作HDFS,使用的是MAVEN,操作的环境是Linux 首先要配置好Maven环境,我使用的是已经有的仓库,如果你下载的jar包 速度慢,可以改变Ma ...
- 【ASP.Net】 http请求中get,put,post,delete的区别与使用总结
在web api的设计上, 需要设计这个每个action对应的资源的请求方法是什么. Get方法是对服务器资源的请求获取, 一般get方法的参数都放在URL当中的. 所以通常情况下这种请求方式都是不安 ...