2014第一篇之膜拜鑫神搜索大神之CF的C题
2014-01-02 19:47
477 查看
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 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.
Sample test(s)
Input
Output
Input
Output
[/code]
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 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.
Sample test(s)
Input
3 4 2 #..# ..#. #...
Output
#.X# X.#. #...
Input
5 4 5 #... #.#. .#.. ...# .#.#
Output
#XXX #X#. X#.. ...# .#.# 大意很简单,n*m的迷宫里面"."代表可以走,"#"代表初始就有的墙,然后现在给你K堵墙让你去堵住一些点,但是仍然保证剩下的点是连通的,当然初始情况下的点是可以任意 到达的,一开始我的思路就是每次用DFS搜一遍然后判断每一个"."四连通方向上有多少个直接相连的"."然后从相连个数最小的开始堵起,后面发现每次搜索完之后还得更新加上 排序实现复杂,后来鑫神说可以换一种思路,就是从一个点开始走起,然后直到走到某个点路不通了,那么就把当前位置变成“X”,然后回到上一个位置继续进行搜索,直到所 给的K堵墙你全部用完了,剩下来的点必然连通。然后我就这样去敲了,运气不错直接过了,在此膜拜搜索大神鑫神~下面贴上代码,写挫了勿怪~[code]#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; char s[505][505]; bool vis[505][505]; int x[4]={1,0,-1,0}; int y[4]={0,1,0,-1}; int k; int n,m; void dfs(int a,int b) { for(int i=0;i<4;i++) { int nx=a+x[i]; int ny=b+y[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&s[nx][ny]=='.') { vis[nx][ny]=1; dfs(nx,ny); } } if(k==0) return ; k--; s[a][b]='X'; } int main() { int flag=0; cin>>n>>m>>k; for(int i=0;i<n;i++) { cin>>s[i]; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(s[i][j]=='.') { dfs(i,j); flag=1; break; } } if(flag==1) break; } for(int i=0;i<n;i++) { cout<<s[i]<<endl; } return 0; }
[/code]
相关文章推荐
- java中的java.lang.NumberFormatException异常
- linux 线程同步
- prophecy about automotive tech
- Hello Netgen - 遵循LGPL开源协议的网格剖分程序
- Enumerable和yield
- 收藏一个关于mybatis比较详细的教程.
- 心的体会之Response 返回xml,json
- 选择排序
- Word文档合并
- Command返回的阅读器的作用
- 通用有限元软件简介及比较
- .NET Remoting 应用实例
- 编程之美--寻找发帖“水王”的扩张问题--代码实现
- 开发库 C++
- sicily Check if a binary tree satisfies the binary search tree property
- AD中用户帐户属性userAccountControl
- 如何用VB.net快速读取较大的xml文件
- WPF的TreeView绑定数据库后的点击问题
- 产生随机数的方式
- Android之SharedPreferences详解