题干:

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 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

Input

3 4 2
#..#
..#.
#...

Output

#.X#
X.#.
#...

Input

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

Output

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

题目大意:

给一个n*m的矩阵,'#'代表墙,'.'代表通路,给一个k,代表需要你将矩阵中k个通路改为X,但是要保证剩下的点依然联通,然后输出改变后的矩阵。

解题报告:

搜索。还有一种思路去搜索,那就是 设有s个 ' . ' ,于是只搜s-k个点,然后就停止,最后把剩下的没搜的都变成 ' X ' 就好了。

AC代码1:(46ms)

#include<bits/stdc++.h>using namespace std;
int n,m,K;
char maze[550][550];
int vis[550][550];
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
void dfs(int x, int y) {int tx,ty;for(int k = 0; k<4; k++) {tx = x+nx[k];ty = y+ny[k];if(tx <1 || tx > n || ty < 1 || ty > m) continue;if(maze[tx][ty] == '#' || vis[tx][ty]!=0) continue;vis[tx][ty] = 1;dfs(tx,ty);}if(K>0) {K--;vis[x][y]=666;}
}
int main()
{cin>>n>>m>>K;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '.' && K > 0) dfs(i,j);}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(vis[i][j]!=666)printf("%c",maze[i][j]);elseprintf("X");}printf("\n");}return 0;} 

WA代码2:(WA3了)

#include<bits/stdc++.h>using namespace std;
int n,m,K;
char maze[550][550];
int vis[550][550];
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
void dfs(int x, int y) {int tx,ty;for(int k = 0; k<4; k++) {tx = x+nx[k];ty = y+ny[k];if(tx <1 || tx > n || ty < 1 || ty > m) continue;if(maze[tx][ty] == '#' || vis[tx][ty]==1) continue;vis[tx][ty] = 1;dfs(tx,ty);}if(K>0) {K--;vis[x][y]=666;}
}
int main()
{cin>>n>>m>>K;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '.' && K > 0) dfs(i,j);}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(vis[i][j]!=666)printf("%c",maze[i][j]);elseprintf("X");}printf("\n");}return 0;} 

AC代码3:(31ms)

#include<bits/stdc++.h>using namespace std;
int n,m,K;
char maze[550][550];
int vis[550][550];
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
void dfs(int x, int y) {int tx,ty;for(int k = 0; k<4; k++) {tx = x+nx[k];ty = y+ny[k];if(tx <1 || tx > n || ty < 1 || ty > m) continue;if(maze[tx][ty] == '#' || vis[tx][ty]==1) continue;vis[tx][ty] = 1;dfs(tx,ty);}if(K>0) {K--;maze[x][y] = 'X';}
}
int main()
{cin>>n>>m>>K;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '.' && K > 0) dfs(i,j);}}for(int i = 1; i<=n; i++) {printf("%s",maze[i]+1);printf("\n");}return 0;} 

【Codeforces - 378C】Maze(dfs,思维)相关推荐

  1. codeforces 378C MAZE

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

  2. Monopole Magnets CodeForces - 1345D(dfs+思维)

    A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exi ...

  3. Last Theorem CodeForces - 1325F(dfs树找最大环+思维)

    It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, ...

  4. Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) dfs + 思维

    传送门 文章目录 题意: 思路: 题意: 给一张图,求必须经过aaa点和bbb点的路径条数. 思路: 通过观察我们发现,这个路径无非就是x−>a−>b−>yx->a->b ...

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

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

  6. Codeforces Round #607 (Div. 2) E. Jeremy Bearimy dfs + 思维

    传送门 文章目录 题意: 思路: 题意: 给你2∗k2*k2∗k个点的一棵树.定义GGG为任选kkk组不同的点,每组点的距离和的最小值.定义BBB为任选kkk组不同的点,每组点的距离和的最大值.让你求 ...

  7. CodeForces - 1323B Count Subrectangles(思维)

    题目链接:点击查看 题目大意:给出一个数组 a 和数组 b 只由 0 和 1 构成,构造出矩阵 maze[ x ][ y ] = a[ x ] * b[ y ],显然maze矩阵同样只由 0 和 1 ...

  8. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

  9. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

最新文章

  1. ubuntu9.10中更改启动顺序(grub2的配置)
  2. 程序员必备的 10 大 GitHub 仓库
  3. CVE-2014-4877 wget: FTP Symlink Arbitrary Filesystem Access
  4. 朵朵糖故事机器人怎么更新_“故事贩卖机”专栏创始人温酒的新作,奇幻世界的暖心物语很治愈...
  5. oracle 论坛 千万级表,Oracle千万级记录操作总结
  6. eclipse 中使用Git
  7. Delphi 10.3.1安装cnpack后,出现错误CnWizards_D103R.dll
  8. 部署laravel项目报错:No input file specified.的解决办法
  9. Python爬取网易云音乐歌手歌曲和歌单!推荐好听的歌吗?
  10. Leetcode 460. LFU 缓存
  11. 计算机怎么解除c盘用户权限,电脑c盘文件夹拒绝访问怎么办 删除c盘文件如何获得管理员权限...
  12. Linux 运维必备的 13 款实用工具,拿好了
  13. 【知识图谱】实践篇——基于知识图谱的《红楼梦》人物关系可视化及问答系统实践:part7项目优化与打包
  14. 当前中国计算机硬件发展情况,中国计算机硬件技术发展与展望.doc
  15. Go语言头秃之路(五)
  16. SpaceX在法兰克福成立Starlink有限公司​
  17. 基于Android实现之智慧记单词APP【100011028】
  18. OpenHarmony适配移植:X86、ARM、RISC-V、MIPS、LoongArch芯片架构简析
  19. 美国确诊超100万!教你用Python画出全球疫情动态图
  20. 一个对战程序的C++源代码

热门文章

  1. 318. Maximum Product of Word Lengths
  2. [爬虫][python][入门][网页源码][百度图片][豆瓣TOP250]
  3. [剑指offer]面试题第[2]题[JAVA][替换空格][函数][字符串]
  4. ajax轮询模拟websocket,Ajax轮询和SSE服务器推送数据与websocket模式的区别性学习
  5. oracle禁止修改密码,Oracle 用户密码过期后不允许修改密码的示例代码
  6. php代码expl,php – 参数号无效:参数未定义Explination
  7. java 类及对象的课后作业_JAVA类和对象课后作业
  8. mysql 载入主体时出错_mysql遇到load data导入文件数据出现1290错误的解决方案
  9. wordl流程图怎么写字_DLG的制作流程图
  10. Linux Kbuild文档 2