codeforces 377 A Maze

codeforces 377A
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 十分喜欢迷宫这个游戏 ,然后他现在给出一个n行m列的迷宫,要求你添加k个X 但是要求你天加完之后使得剩下的’.'是一个连通块;让你求出这个矩阵;答案可能有多组输出其中一组即可

题解
遍历每一个点,从4个方向分别dfs 经过的点标记一下要是该点不能向四周遍历的话就直接把该点设置为’X‘即可;
代码

#include<bits/stdc++.h>
using namespace std;
char mpp[505][505];
int vis[505][505];
int n,m,k;
void dfs(int x,int y)
{
   if(x<0||x>=n||y<0||y>=m) return ;
    if(vis[x][y]!=0)return ;
     if(mpp[x][y]!='.')  return ;
     vis[x][y]=1;
    dfs(x-1,y);
    dfs(x+1,y);
    dfs(x,y+1);
    dfs(x,y-1);
   if(k) {mpp[x][y]='X';k--;}
}

int main()
{
   cin.tie(0);
ios::sync_with_stdio(0);
    cout.tie(0);
  cin>>n>>m>>k;
    memset(vis,0,sizeof(vis));
   for(int i=0;i<n;i++)
   cin>>mpp[i];
    for(int i=0;i<n;i++)
       for(int j=0;j<m;j++)
         {
             if(k)
             dfs(i,j);
         }
   for(int i=0;i<n;i++)
   cout<<mpp[i]<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_40623603/article/details/84900910