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 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.
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.
Example
3 4 2
#..#
..#.
#...
#.X#
X.#.
#...
5 4 5
#...
#.#.
.#..
...#
.#.#
#XXX
#X#.
X#..
...#
.#.# 一开始用的广搜,不对,广搜没啥合理性,实在是找不出道理,但是深搜可以,深搜可以把所有点连成一棵树,按顺序删除一些结点不会影响连通性,所以用递归
dfs直到四个方位都遍历过了,再把这个结点删除。 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
int n,m,k,rc=,sx,sy;
int dir[][]={,,,,,-,-,};
char mp[][];
int vis[][];
void dfs(int x,int y)
{
if(x<||y<||x>=n||y>=m)return;
if(mp[x][y]=='#'||vis[x][y])return ;
vis[x][y]=;
for(int i=;i<;i++)
{
int tx=x+dir[i][];
int ty=y+dir[i][];
dfs(tx,ty);
}
if(k)mp[x][y]='X',k--;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>m>>k;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='.')sx=i,sy=j;
else vis[i][j]=;
}
} dfs(sx,sy);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
putchar(mp[i][j]); putchar('\n');
}
}
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) ...
- 基于上三角变换或基于DFS的行(列)展开的n阶行列式求值算法分析及性能评估
进入大一新学期,看完<线性代数>前几节后,笔者有了用计算机实现行列式运算的想法.这样做的目的,一是巩固自己对相关概念的理解,二是通过独立设计算法练手,三是希望通过图表直观地展现涉及的两种算 ...
- 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,问怎么变才能使得剩下的空格依然 ...
- POJ 3083 BFS+DFS 40行
题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...
- CodeForces 378C Maze (DFS)
题目链接 题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案. 分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘ 一直填充k个 ...
- Magic Maze dfs + dp
http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...
- 选课 树形DP+多叉树转二叉树+dfs求解答案
问题 A: 选课 时间限制: 1 Sec 内存限制: 128 MB 题目描述 大 学里实行学分.每门课程都有一定的学分,学生只要选修了这门课并考核通过就能获得相应的学分.学生最后的学分是他选修的各门 ...
- poj3764(dfs+Trie树+贪心)
题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...
随机推荐
- Java 常用对象-System类
2017-11-02 21:41:06 System类:System 类包含一些有用的类字段和方法.它不能被实例化. *常用方法 public static void gc() 运行垃圾回收器. 调用 ...
- PHP自带调试函数
1.var_dump:打印变量的相关信息 $a = array(1, 2, array("a", "b", "c")); var_dump( ...
- codeforces 516c// Drazil and Park// Codeforces Round #292(Div. 1)
题意:一个圆环上有树,猴子上下其中一棵树,再沿着换跑,再上下另一棵树.给出一个区间,问最大的运动距离是. 给出区间大小dst,和数高数组arr. 设区间[x,y],a[x]=2*arr[x]+dst[ ...
- Android设计模式之工厂模式
定义 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见.因为工厂模式就相当于创建实例 ...
- Zepto常见问题
Zepto遇到的问题 延时 总所周知,默认click事件,会有300ms的延时问题,Zepto的tap事件很好的解决了这个问题.但是也挖了一个大坑. 点透 Zepto的点透应该也是大家比较熟悉的了.当 ...
- 为tomcat指定jdk
安装好jdk,不配置环境变量 windows平台 在catalina.bat文件和setclasspath.bat文件开头的空白处加上如下两句(指定JDK): \jre linux平台 在catali ...
- ssh: connect to host 192.168.11.180 port 22: Connection refused
错误原因: 1.sshd 未安装:sudo apt-get install openssh-server 2.sshd 未启动:sudo net start sshd 3.防火墙:sudo ufw d ...
- 关于floyd 打印路径的问题
我们令 f[i][j] 表示从 i-->j的最短路上j前面的那个点. 显然初始化时 f[i][j]=i; (这样的话先判断一下i是否能到达j好点) 更新条件时,当发现通过点k能使最短 ...
- zzuli1731 矩阵(容斥)
1731: 矩阵 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 600 Solved: 106 SubmitStatusWeb Board Descr ...
- throw new OAException执行了,却没有正常抛出异常!
try { if ("E".equals(returnStatus)) { throw new OAException(returnMessage, OAException.ERR ...