Codeforces Round #222 (Div. 1) A. Maze dfs

A. Maze

题目连接:

http://codeforces.com/contest/377/problem/A

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

3 4 2

..

..#.

Sample Output

.X

X.#.

Hint

题意

在一个nm的矩阵里面,你需要画k个’X’使得,剩下的.都在一个连通块里面

题解:

我们这么想,我们只要按照dfs的顺序去涂X就好了

如果一开始就有多个连通块的话,我们最后剩下的是最大的连通块,其他的一定都可以被填满的

然后再dfs去填就好了

代码

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y,z,k;
};
const int maxn = 505;
int n,m,k;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
char mp[maxn][maxn];
int vis[maxn][maxn];
int cnt=1,sum=0;
vector<node>P;
bool cmp(node a,node b)
{
    return a.k>b.k;
}
void dfs(int x,int y)
{
    sum++;
    vis[x][y]=cnt;
    for(int i=0;i<4;i++)
    {
        int xx = x+dx[i];
        int yy = y+dy[i];
        if(xx<1||xx>n)continue;
        if(yy<1||yy>m)continue;
        if(vis[xx][yy])continue;
        if(mp[xx][yy]=='#')continue;
        dfs(xx,yy);
    }
}
void dfs2(int x,int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int xx = x+dx[i];
        int yy = y+dy[i];
        if(xx<1||xx>n)continue;
        if(yy<1||yy>m)continue;
        if(vis[xx][yy])continue;
        if(mp[xx][yy]=='#')continue;
        dfs2(xx,yy);
    }
    if(k>0){
        mp[x][y]='X';
        k--;
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++)
        scanf("%s",mp[i]+1);
    node tmp;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]=='.'&&vis[i][j]==0)
            {
                sum=0;
                dfs(i,j);
                tmp.x=i,tmp.y=j,tmp.z=cnt,tmp.k=sum;
                cnt++;
                P.push_back(tmp);
            }
        }
    }
    if(cnt==1)
    {
        for(int i=1;i<=n;i++,cout<<endl)
            for(int j=1;j<=m;j++)
                cout<<mp[i][j];
        return 0;
    }
    sort(P.begin(),P.end(),cmp);
    int ans1 = P[0].z,ans2 = k;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]=='#')continue;
            if(vis[i][j]!=ans1)
            {
                mp[i][j]='X';
                k--;
            }
        }
    }
    memset(vis,0,sizeof(vis));
    dfs2(P[0].x,P[0].y);
    for(int i=1;i<=n;i++,cout<<endl)
    {
        for(int j=1;j<=m;j++)
        {
            cout<<mp[i][j];
        }
    }
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5541259.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞