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.
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.
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.
3 4 2 #..# ..#. #...
#.X# X.#. #...
5 4 5 #... #.#. .#.. ...# .#.#
#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;
}