D - Maze(深度搜索+思维转换)

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 exactlyk 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 letters 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
Input
3 4 2
#..#
..#.
#...
Output
#.X#
X.#.
#...
Input
5 4 5
#...
#.#.
.#..
...#
.#.#
Output
#XXX
#X#.
X#..
...#
.#.#

 

我是copy的:如下

就是从一个点开始走起,然后直到走到某个点路不通了,那么就把当前位置变成“X”,然后回到上一个位置继续进行搜索,直到所
给的K堵墙你全部用完了,剩下来的点必然连通。

 
  
  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. using namespace std;  
  7.   
  8. char s[505][505];  
  9. bool vis[505][505];  
  10. int x[4]={1,0,-1,0};  
  11. int y[4]={0,1,0,-1};  
  12. int k;  
  13. int n,m;  
  14. void dfs(int a,int b)  
  15. {  
  16.     for(int i=0;i<4;i++)  
  17.     {  
  18.         int nx=a+x[i];  
  19.         int ny=b+y[i];  
  20.         if(nx>=0&&nx=0&&ny'.')  
  21.         {  
  22.             vis[nx][ny]=1;  
  23.             dfs(nx,ny);  
  24.   
  25.         }  
  26.   
  27.     }  
  28.     if(k==0)  
  29.         return ;  
  30.     k--;  
  31.     s[a][b]='X';  
  32.   
  33.   
  34. }  
  35. int main()  
  36. {   int flag=0;  
  37.     cin>>n>>m>>k;  
  38.     for(int i=0;i
  39.     {  
  40.         cin>>s[i];  
  41.     }  
  42.     for(int i=0;i
  43.     {  
  44.         for(int j=0;j
  45.         {  
  46.             if(s[i][j]=='.')  
  47.             {  
  48.                 dfs(i,j);  
  49.                flag=1;  
  50.                 break;  
  51.             }  
  52.         }  
  53.         if(flag==1)  
  54.             break;  
  55.     }  
  56.     for(int i=0;i
  57.     {  
  58.         cout<
  59.     }  
  60.     return 0;  
原文地址:http://blog.csdn.net/u011481752/article/details/17763925

还是得好好学 不够扎实。


你可能感兴趣的:(搜索)