Codeforces Round #222 (Div. 1)A. Maze(深搜)

传送门

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 ≤ 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
#..#
..#.
#...


5 4 5
#...
#.#.
.#..
...#
.#.#

Sample Output

#.X#
X.#.
#...

#XXX
#X#.
X#..
...#
.#.#

思路

题意:

 给出一个迷宫,'.' 表示通路,‘#' 表示城墙,输入的数据中,‘.’构成的已经联通,要求将k个'.'转换成'#',使得剩下的'.'仍然是联通的

题解:

 深搜,假设共有s个'.'那么搜索得到的s-k个点必然是联通的,那么只要将剩下的'.'转换成'#'就行了。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
char maze[maxn][maxn];
bool vis[maxn][maxn];
int n, m, k;
int dx[4] = {0, -1, 0, 1};
int dy[4] = { -1, 0, 1, 0};


void dfs(int mx, int my)
{
    vis[mx][my] = 1;
    for (int i = 0; i < 4; i++)
    {
        int nx = mx + dx[i], ny = my + dy[i];
        if (0 <= nx && nx < n && 0 <= ny && ny < m && maze[nx][ny] == '.' && !vis[nx][ny])
        {
            dfs(nx, ny);
        }
    }
    if (k > 0)  maze[mx][my] = 'X',k--;
}

int main()
{
    memset(vis, 0, sizeof(maze));
    bool flag = false;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < n; i++)   scanf("%s", maze[i]);
    for (int i = 0; i < n && !flag; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (maze[i][j] == '.')
            {
                dfs(i,j);
                flag = true;
                break;
            }

        }
    }
    for (int i = 0;i < n;i++)   printf("%s\n",maze[i]);
    return 0;
}
 
 
 
posted @   zxzhang  阅读(347)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航