DFS找末端的空地变成墙。。。。。。

A. Maze
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains three integers nmk (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.

Sample test(s)
input
3 4 2
#..#
..#.
#...

output
#.X#
X.#.
#...

input
5 4 5
#...
#.#.
.#..
...#
.#.#

output
#XXX
#X#.
X#..
...#
.#.#

#include <iostream>
#include <cstdio>
#include <cstring>using namespace std;char mp[512][512];
bool vis[512][512];int n,m,k;void dfs(int x,int y)
{if(x<0||x>=n||y<0||y>=m) return ;if(mp[x][y]!='.') return ;if(vis[x][y]) return ;vis[x][y]=true;dfs(x+1,y); dfs(x-1,y);dfs(x,y+1); dfs(x,y-1);if(k) mp[x][y]='X',k--;
}int main()
{scanf("%d%d%d",&n,&m,&k);for(int i=0;i<n;i++) scanf("%s",mp[i]);for(int i=0;i<n&&k;i++){for(int j=0;j<m&&k;j++){dfs(i,j);}}for(int i=0;i<n;i++) puts(mp[i]);return 0;
}

CodeForces 377 A. Maze相关推荐

  1. 【Codeforces - 378C】Maze(dfs,思维)

    题干: Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, ...

  2. codeforces 378C MAZE

    D - Maze Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. Codeforces Round #377 (Div. 2) E. Sockets

    http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...

  4. Solve The Maze CodeForces - 1365D(贪心+dfs)

    Vivek has encountered a problem. He has a maze that can be represented as an n×m grid. Each of the g ...

  5. D. Solve The Maze Codeforces Round #648 (Div. 2)

    D. Solve The Maze Vivek has encountered a problem. He has a maze that can be represented as an n×m g ...

  6. Codeforces Round #377 (Div. 2) 732A B C D E F

    蒟蒻只能打div 2 A题水,10和个位数的使用互不影响,所以直接在模10意义下做 1 #include<bits/stdc++.h> 2 using namespace std; 3 i ...

  7. CodeForces - 1293C NEKO's Maze Game(思维,水题)

    题目链接:点击查看 题目大意:给出一个2*n大小的矩阵,现在有m次操作,每次操作将某一个方格的状态置反,这里的每个方块都有两种状态,一种状态是可通行状态,另一种是不可通行状态,初始时所有方块都是可通行 ...

  8. Codeforces Round #222 (Div. 2): C. Maze(BFS)

    题意: 给你一个n*m的迷宫,'.'是路,'#'是墙,输入保证所有的'.'构成一个联通块,要求为这个迷宫再添加k面墙,使得剩下所有的'.'仍然构成一个联通块 思路:反过来处理,先将所有的'.'全部变成 ...

  9. codeforces D. Solve The Maze

    题目 题意: 你有一个地图,总共有 4 4 4种符号,'.','#',' G G G',' B B B',分别代表着路,墙,好人,坏人,现在你可以将路变成墙,问最后是否有一种方案使得坏人走不到 n , ...

最新文章

  1. 关于truncate与checkident
  2. Zoom Capability
  3. 【翻译】在Sencha应用程序中使用插件和混入
  4. mysql sql with_mysql5.7 查询sql 出错: with sql_mode=only_full_group_by
  5. 【小白学习PyTorch教程】九、基于Pytorch训练第一个RNN模型
  6. wxWidgets:wxPGProperty类用法
  7. html2canvas解决保存图片模糊问题
  8. java的uuid有什么用_java – 什么是有效的UUID?
  9. Jeecg社区wiki在开放,终于可以在线看文档啦!!!
  10. pytorch自定义loss损失函数
  11. matlab钢琴音教程,弹琴吧 - 今天聊一聊钢琴扒带及MIDI制作,使用音
  12. java求1到100素数的和_求解JAVA题:求出1~100中所有质数?,求一段java代码,题目是求1到100之间所有质数和?...
  13. TensorFlow2.0 学习笔记(五):循环神经网络(RNN)
  14. FMAN(Fully Motion-Aware Network for Video Object Detection)论文详读
  15. 推荐一款团队协作软件---confluence
  16. java开发的公文管理系统源代码_基于jsp的公文管理系统-JavaEE实现公文管理系统 - java项目源码...
  17. lua服务器客户端消息回调,lua服务器客户端消息回调
  18. 【proteus仿真】89C51单片机实现十字路口的交通灯控制系统代码
  19. 大功率mos管(功率mos管)的五种损坏原因分析,新手必读
  20. AcWing——第55场周赛

热门文章

  1. Tomcat日志乱码解决
  2. UG NX 12 基准平面
  3. 关于primer的学习
  4. 池馆刷题助手一键答题显示答案综合教程交互英语
  5. 【swift】swift quick start
  6. H5移动端的一些坑、、、
  7. linux系统能否在pe里装,原来如此,Linux系统也有PE,不过它叫...
  8. Guard Manager
  9. e签宝签署电子合同使用手册
  10. IDEA启动Tomcat之后不能访问localhost的解决方案