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)来表示图. ...
随机推荐
- php赋值运算符
= 赋值 += $x+=3相当于$x = $x+3; -= *= /+ %= .=
- div的浮动(float)
什么是浮动 浮动,故名思议,就是移动位置. 之所以不叫移动,而叫浮动,那是因为给元素设置浮动后,元素会浮到文档上面来,术语叫脱离文档流. 例子 下面我们看例子 <html> <hea ...
- (开发)ESLint - 代码规范
参考文档:http://eslint.cn/ ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 J ...
- Windows下hosts文件的作用
原文地址:https://my.oschina.net/u/874225/blog/194348 在操作系统中的路径:Win7在C:\Windows\System32\drivers\etc目录下 内 ...
- Verilog分频器的设计
大三都要结束了,才发现自己太多东西没深入学习. 对于偶分频:(计数到分频数的一半就翻转) 注: 图中只用了一个计数器,当然也可以用多个: 图中只计数到需要分频的一半,当然也可计数到更多: 图中从第一个 ...
- 如何在CRM WebClient UI里使用HANA Live Report
1. 使用业务角色ANALYTICSPRO登录SAP CRM WebClient UI: 点击新建按钮创建一个新的HANA live report: 类型选择SHL: 弹出窗口,维护report的名称 ...
- 最长公共单词,类似LCS,(POJ2250)
题目链接:http://poj.org/problem?id=2250 解题报告: 1.状态转移方程: ; i<=len1; i++) { ; j<=len2; j++) { dp[i][ ...
- CentOS 6下PXE+Kickstart无人值守安装操作系统
一.简介1.1 什么是PXEPXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作 ...
- 2017.10.18 微机原理与接口----汇编语言语法和DOS功能调用
4.1 汇编语言中的基本数据 ·标识符 ·常数 ·变量具有三个属性: (1)段地址(SEG):变量所在段的段地址 (2)偏移地址(OFFSET):变量所在段内的偏移地址 (3)类型(TYPE):每个变 ...
- 用keytool制作证书并在tomcat配置https服务(三)
用keytool制作证书并在tomcat配置https服务(一) 用keytool制作证书并在tomcat配置https服务(二) 用keytool制作证书并在tomcat配置https服务(四) 模 ...