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 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.
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
Copy
3 4 2 #..# ..#. #...
output
Copy
#.X# X.#. #...
input
Copy
5 4 5 #... #.#. .#.. ...# .#.#
output
Copy
#XXX #X#. X#.. ...# .#.#
题目大致意思:给你一个n行m列的图,"#"为墙,"."为陆地,初始联通陆地只有一块,让你填入k个"X" 墙后,联通陆地还是一块。
主要考点为深搜。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"
const ll N = 1e3+7;
ll n,m,k;
string s[N];
bool vis[N][N];
ll dir[4][2]={0,1,1,0,0,-1,-1,0};
void dfs(ll x ,ll y){
vis[x][y]=1;
for(ll i = 0 ; i < 4 ; i ++){
ll tx = x + dir[i][0];
ll ty = y + dir[i][1];
if(tx < 0 || ty < 0 || tx >= n || ty >= m || s[tx][ty] != '.' || vis[tx][ty])continue;
dfs(tx,ty);
}
if(k > 0){
k--;
s[x][y]='X';
}
return;
}
void solve(){
cin >> n >> m >> k;
for(ll i = 0 ; i < n ; i ++)cin >> s[i];
ll x=0,y=0;
bool f=0;
for(; x < n ; x ++){
if(f)break;
for(y = 0 ; y < m ; y ++){
if(s[x][y]=='.')f=1;
if(f)break;
}
}
dfs(x,y);
for(ll i = 0 ; i < n ; i ++)cout << s[i] << endl;
return;
}
int main(){
ll t=1;//cin >> t;
while(t--)solve();
return 0;
}
编程题目要求在保持连通性的前提下,通过DFS算法在网格迷宫中添加k个墙壁,输出新的迷宫布局。
737

被折叠的 条评论
为什么被折叠?



