Codeforces 377 A. Maze(搜索技巧)

本文介绍了一种算法,用于在一个联通的迷宫中增加指定数量的墙壁,确保迷宫仍然保持联通状态。通过广度优先搜索(BFS)来实现墙壁的有效放置。

A. Maze

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
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.

Sample Input

Input
3 4 2
#..#
..#.
#...
Output
#.X#
X.#.
#...
Input
5 4 5
#...
#.#.
.#..
...#
.#.#
Output
#XXX
#X#.
X#..
...#
.#.#
题目大意:给你n,m和k,n和m代表矩阵的大小,然后k是需要填的数量。有一个人有一个联通的房间,'#'代表墙,'.'代表可以走的路,然后再往图中填k个墙壁之后图还是联通的,输出填的方案。
题目解析:我们随意找一个为空地的地方开始跑bfs,跑到bfs跑不到的地方结束,按照bfs序列我们得到了一个类似于栈的序列,我们再依次按照bfs序列把空地填回去就可以了。
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Pair pair<int,int>
const int INF = 0x3f3f3f3f;
using namespace std;

char arr[505][505];
bool vis[505][505];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

int main(){
    int n,m,k,sx,sy;
    while(~scanf("%d %d %d",&n,&m,&k)){
        stack<Pair>S;
        queue<Pair>Q;
        memset(vis,false,sizeof(vis));
        sx = sy = -1;
        for(int i=0;i<n;i++){
            scanf("%s",arr[i]);
            if(sx == -1){
                for(int j=0;j<m;j++){
                    if(arr[i][j] == '.'){
                        sx = i;sy = j;
                        break;
                    }
                }
            }
        }
        vis[sx][sy] = true;
        S.push(make_pair(sx,sy));
        Q.push(make_pair(sx,sy));
        while(!Q.empty()){
            Pair ant = Q.front();Q.pop();
            for(int i=0;i<4;i++){
                int nowx = ant.first  + dx[i];
                int nowy = ant.second + dy[i];
                if(nowx >= 0 && nowx < n && nowy >=0 && nowy < m && arr[nowx][nowy] == '.' && !vis[nowx][nowy]){
                    vis[nowx][nowy] = true;
                    S.push(make_pair(nowx,nowy));
                    Q.push(make_pair(nowx,nowy));
                }
            }
        }
        while(!S.empty()){
            if(k){
                Pair ant = S.top();S.pop();
                if(arr[ant.first][ant.second] == '.'){
                    arr[ant.first][ant.second] = 'X';
                    k -= 1;
                }
            }
            else{
                break;
            }
        }
        for(int i=0;i<n;i++){
            printf("%s\n",arr[i]);
        }
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值