• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #222 Div2 C

C. Maze
time limit per test 2 seconds
memory limit per test 256 megabytes
添加X还要最后连通,等价于直接找最后连通剩余数!注意为0个X

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.

Sample test(s)
Input
3 4 2 
#..#
..#.
#...
Output
#.X# 
X.#.
#...
Input
5 4 5 
#...
#.#.
.#..
...#
.#.#
Output
#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 }
View Code
posted @ 2014-01-01 12:58  HaibaraAi  阅读(158)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3