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 <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cmath>
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<<G[i][j];
    cout<<endl;
    }
}
//更新矩阵
//如果找到了答案,就把剩余的空格变为'X',否则把所有空格的访问标识置零
void Updata(int flag){
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++){
        if(flag){
            if(!vis[i][j]) G[i][j]='X';
        }
        else if(G[i][j]=='.') vis[i][j]=0;
    }
}
bool dfs(int r,int c){
    vis[r][c]=1;
    sumOfPoint++;//每到一个空格 个数加一
    if(sumOfPoint==sumOfEcell) return true;
    for(int i=0;i<4;i++){
        if(!vis[r+dr[i]][c+dc[i]]&&isCan(r+dr[i],c+dc[i])&&G[r+dr[i]][c+dc[i]]=='.')
            if(dfs(r+dr[i],c+dc[i])) return true;//找到答案
    }
    return false;
}
void solve(){
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++)
    if(!vis[i][j]){
        sumOfPoint=0;
        if(dfs(i,j)){
            Updata(1);
            print();
            return;//找到解退出
        }
        else Updata(0);
    }
}
int main(){
    mem(G);mem(vis);
    while(cin>>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();
    }
}


猜你喜欢

转载自blog.csdn.net/insist_77/article/details/79780304