codeforces 378C MAZE

来源:互联网 发布:双色球旋转矩阵计算器 编辑:程序博客网 时间:2024/05/18 09:02
D - Maze
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 377A

Description

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 ≤ 5000 ≤ 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 Input

Input
3 4 2#..#..#.#...
Output
#.X#X.#.#...
Input
5 4 5#...#.#..#.....#.#.#
Output
#XXX#X#.X#.....#.#.#
思路:感觉这是一道比较经典的题目,很考验我个人对搜索的理解,题意就是要求我们给你一个图.表示通路,
#表示墙,
给你k,让你放k个墙用x表示,使得到的图仍然为一个通路;
我对这个题的做法就是;按四个方向搜索,然后每次搜索到边界回溯的时候就开始放X,知道k等于0的时候,
我把每个放x的地方在标记数组里标记成2,最后输出的时候处理一下就好
//还有个问题就是这个题不需要复位的,就是标记数组复位,因为题中本来就是通路,由一点出发经过四个方向
一定可以遍历这个通路的,而且一遍就可以放完所有的X不需要再走,我们所说的那种标记数组复位一般就是此路
不通,需要按原路返回寻找其他满足题意的通路时才要复位。
//我对复位与否理解的也不是很深,希望有人可以指导一下;
ac代码:
#include<stdio.h>#include<string.h>char a[510][510];int b[510][510];int n,m,k;int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}};void dfs(int x,int y){     int i,tx,ty;         for(i=0;i<4;i++)     {    tx=x+next[i][0];          ty=y+next[i][1];         if(tx>=0&&tx<n&&ty>=0&&ty<m&&b[tx][ty]==0&&a[tx][ty]=='.')              {  b[x][y]=1;  dfs(tx,ty);}     }     if(k)     {  --k;       b[x][y]=2;     }}int main(){    int i,j;     memset(b,0,sizeof(b));     scanf("%d %d %d",&n,&m,&k);     for(i=0;i<n;i++)       scanf("%s",a[i]);     int fx,fy;     for(i=0;i<n;i++)      for(j=0;j<m;j++)       {    if(a[i][j]=='.')              {  fx=i;                 fy=j;                 break;              }       }        b[fx][fy]=1;       dfs(fx,fy);       for(i=0;i<n;i++)        {         for(j=0;j<m;j++)         {  if(b[i][j]==2)             printf("X");             else             printf("%c",a[i][j]);         }         printf("\n");        }         return 0;
}
0 0
原创粉丝点击