17-比赛2 C - Maze (dfs)
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 n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where nand 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
3 4 2
#..#
..#.
#...
#.X#
X.#.
#...
5 4 5
#...
#.#.
.#..
...#
.#.#
#XXX
#X#.
X#..
...#
.#.# 题意:
插入 k 个 X 且保持所有的 '.' 保持贯通
====================================================================================================================================================
怎么样才能使插入的 X 没有阻断路的连通,,想一想DFS,不撞南墙不回头的理论,只要沿着一个点一路走到底,直到不能走为止,那么这个末端放上X一定不会阻断其它'.'的连贯
如果所有末端 插入完毕之后,还有X没有放入,那么末端之后紧挨着的点就变成了末端,根据DFS走到末端后会原路返回,那么剩下的X跟着原路返回时插入就行了。
====================================================================================================================================================
代码:
#include<bits/stdc++.h>
using namespace std;
char Map[][];
bool book[][];
int n,m,k;
void dfs(int x,int y)
{
if(x<||x>=n||y<||y>=m) return;
if(Map[x][y]!='.'||book[x][y]==) return ;
book[x][y]=;
for(int i=;i<=;i++)
{ //四个方向
if(i==) dfs(x+,y);
if(i==) dfs(x-,y);
if(i==) dfs(x,y+);
if(i==) dfs(x,y-);
}
//当遍历到底了之后,即循环了四次无法继续走下去时
if(k!=)
Map[x][y]='X',k--;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<n;++i)
scanf("%s",Map[i]); for(int i=;i<n;++i)
{
for(int j=;j<m;j++)
{
dfs(i,j);
if(k==) break;
}
if(k==) break;
}
for(int i=;i<n;i++)
puts(Map[i]);
return ;
}
17-比赛2 C - Maze (dfs)的更多相关文章
- HDU 1484 Basic wall maze (dfs + 记忆)
Basic wall maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Maze dfs倒行
Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or i ...
- Codeforces Round #222 (Div. 1) A. Maze dfs
A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- CodeForces 378C Maze (DFS)
题目链接 题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案. 分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘ 一直填充k个 ...
- (比赛)B - 棋盘问题(dfs)
B - 棋盘问题 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu Practice POJ ...
- Magic Maze dfs + dp
http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...
- 图的基本算法(BFS和DFS)(转载)
图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...
- BFS和DFS算法
昨晚刚昨晚华为笔试题,用到了BFS和DFS,可惜自己学艺不精,忘记了实现原理,现在借用大佬写的内容给自己做个提高 转自:https://www.jianshu.com/p/70952b51f0c8 图 ...
- BFS(广搜)DFS(深搜)算法解析
图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...
随机推荐
- 从零开始的全栈工程师——js篇2.18(js的运动)
一.元素的 client offset scroll 三个系列 clientWidth / clientHeight / clientTop / clientLeftoffsetWidth / off ...
- mybatis VS hibernate
转自:http://blog.csdn.net/firejuly/article/details/81902 第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/ ...
- RN的打包
1.首先执行以下命令 在android目录下 keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -key ...
- git 推送代码到远程端
git init git add . git commit -m "first commit" git remote add origin "地址" git p ...
- 分析一点python源代码
偶然看了一下python的部分源代码,感觉python的作者写的代码真心很美,简洁美观,学习之. 举几个例子抛砖引玉一下: def removedirs(name): ""&quo ...
- Linux命令之文件重定向2
linux中重定向用符号“>”表示,语法一般是 源文件 > 目标文件 1)创出.txt文件touch 1.txt 注意:创建文件夹用mkdir 2)向.txt文件中写入内容 注意:①cat ...
- Spark master节点HA配置
Spark master节点HA配置 1.介绍 Spark HA配置需要借助于Zookeeper实现,因此需要先搭建ZooKeeper集群. 2.配置 2.1 修改所有节点的spark-evn.sh文 ...
- uva题库爬取
每次进uva都慢的要死,而且一步一步找到自己的那个题目简直要命. 于是,我想到做一个爬取uva题库,记录一下其中遇到的问题. 1.uva题目的链接是一个外部的,想要获取https资源,会报出SNIMi ...
- python遇到IndentationError: unexpected indent
由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可
- GPU和CPU耗时统计方法
GPU端耗时统计 cudaEvent_t start, stop; checkCudaErrors(cudaEventCreate(&start)); checkCudaErrors(cuda ...