A. Maze----暴力

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
3 4 2
#..#
..#.
#...
output
#.X#
X.#.
#...
input
5 4 5
#...
#.#.
.#..
...#
.#.#
output
#XXX
#X#.
X#..
...#
.#.#

題目鏈接:http://codeforces.com/contest/377/problem/A


題目的意思是說讓你把k個.變成X,然後讓剩下的.仍然聯通。

直接暴力,我們設已經改了s個,還要改k-s個,直接暴力

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,k;
int dic[4][2]={0,1,1,0,0,-1,-1,0};
char map1[600][600];
int vis[600][600];
void dfs(int r,int c){
    if(!k){
        return ;
    }
    k--;
    vis[r][c]=1;
    for(int i=0;i<4;i++){
        int tr=r+dic[i][0];
        int tc=c+dic[i][1];
        if(tr>=0&&tr<n&&tc>=0&&tc<m&&!vis[tr][tc]&&map1[tr][tc]=='.'){
            dfs(tr,tc);
        }
    }
}
int main(){
    scanf("%d%d%d%*c",&n,&m,&k);
    int t=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%c",&map1[i][j]);
            if(map1[i][j]=='.')
                t++;
        }
        getchar();
    }
    k=t-k;
    int flag=1;
    for(int i=0;i<n&&flag;i++){
        for(int j=0;j<m&&flag;j++){
            if(map1[i][j]=='.'){
                dfs(i,j);
                flag=0;
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(map1[i][j]=='.'&&!vis[i][j]){
                putchar('X');
            }
            else{
                printf("%c",map1[i][j]);
            }
        }
        cout<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章