CodeForces 377A Maze(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 ≤ 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.

Examples
input
Copy
3 4 2
#..#
..#.
#...
output
#.X#
X.#.
#...
input
Copy
5 4 5
#...
#.#.
.#..
...#
.#.#
output
#XXX
#X#.
X#..
...#
.#.#

分析:

       拿到题目首先想到的是在空格中插‘X’,但是太麻烦而且时间耗费大(我太菜了,不会。。。),换个思路,一共有m*n个格子,其中假设有a个‘#’,还剩下m*n-a个‘.’,要插入k个‘X’,我们只需找到m*n-a-k个联通的空格即可。

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
int m,n,k;
const int maxn = 500+5;
char G[maxn][maxn];//储存矩阵信息
int vis[maxn][maxn];//访问标识
//dfs方向
int dr[4]={1,-1,0,0};
int dc[4]={0,0,-1,1};
int sumOfEcell;//剩余空格的个数
int sumOfPoint;//联通点的个数
//判断是否越界
bool isCan(int r,int c){
    if(1<=r&&r<=m&&1<=c&&c<=n) return true;
    return false;
}
//打印结果
void print(){
    for(int i=1;i<=m;i++){
    for(int j=1;j<=n;j++)
        cout<>m>>n>>k){
        sumOfEcell=m*n-k;
        for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++){
            cin>>G[i][j];
            if(G[i][j]=='#'){
                vis[i][j]=1;
                sumOfEcell--;
            }
        }
        solve();
    }
}


转载于:https://www.cnblogs.com/seven7777777/p/10278753.html

你可能感兴趣的:(CodeForces 377A Maze(dfs))