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)来表示图. ...
随机推荐
- canvas的globalAlphaAPI
canvas的globalAlphaAPI
- yii2.0安装ElasticSearch及使用
yii2.0安装ElasticSearch安装及使用教程:https://www.yiichina.com/tutorial/1046 Elasticsearch 权威指南(中文版):https:// ...
- 诸葛io | 精细化运营分析解决方案
类型: 定制服务 软件包: business intelligence internet media solution collateral 联系服务商 产品详情 解决方案 概要 数据监测 ? 异常发 ...
- Oracle三种循环例题:打印九九乘法表
数据库SQL三种循环语句(For.While.Loop) --如果要将执行结果输出,需要先执行 setserveroutput on 命令,在窗口里显示服务器输出信息 set serveroutput ...
- expres webpack es6 babel 构建多页系统开发架构
开始写点什么... 只是一个思路........
- Docker build 安装报错, Could not open requirments file: [Errno 2] No such file or directory:'requirements.txt'
docker安装教程 https://docs.docker.com/get-started/part2/#build-the-app 相关帖子 https://stackoverflow.com/q ...
- May 7th 2017 Week 19th Sunday
A chain is no stronger than its weakest link. 链条的坚固程度取决于它最薄弱的环节. The same as the well-known buckets ...
- 141. Linked List Cycle (amazon)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- IOS transform的使用(移动,放大,旋转)
@interface ViewController () - (IBAction)up; - (IBAction)big ; - (IBAction)leftRotate ; @property (n ...
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013480600/article/details/32140501 HDU 3639 Hawk-a ...