Codeforce Round #222 Div2 C
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#..
...#
.#.#
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <stack> 6 #include <queue> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define mod 1000000007 13 #define ll long long 14 #define maxn 506 15 #define pi acos(-1.0) 16 #define FF(i,n) for(int i=0;i<n;i++) 17 int n, m, z, t, flag, k; 18 int dx [] = { 1, 0, -1, 0 }; 19 int dy [] = { 0, 1, 0, -1 }; 20 char s[maxn][maxn]; 21 int vis[maxn][maxn]; 22 void dfs(int x, int y){ 23 if (k == z)return; 24 for (int i = 0; i < 4; i++){ 25 int nx = x + dx[i]; 26 int ny = y + dy[i]; 27 if (vis[nx][ny] == 0&&nx>=0&&ny>=0&&nx<n&&ny<m){ 28 vis[nx][ny] = 1; 29 k++; 30 dfs(nx, ny); 31 if (k==z)return; 32 } 33 } 34 } 35 int main(){ 36 scanf("%d%d%d", &n, &m, &t); 37 z = 0; flag = 0; 38 for (int i = 0; i < n; i++){ 39 scanf("%s", s[i]); 40 for (int j = 0; j < m; j++){ 41 if (s[i][j] == '.')z++; 42 else vis[i][j] = -1; 43 } 44 } 45 z -= t; k = 0; 46 for (int i = 0; i < n;i++) 47 for (int j = 0; j < m; j++) 48 if (s[i][j] == '.')dfs(i, j), j = m, i = n; 49 if (t){ 50 for (int i = 0; i < n; i++) 51 for (int j = 0; j < m; j++){ 52 if (vis[i][j] == 0)s[i][j] = 'X'; 53 } 54 } 55 for (int i = 0; i < n; i++)printf("%s\n", s[i]); 56 }
浙公网安备 33010602011771号