codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

723D.md (2474B)


      1 Source: https://codeforces.com/problemset/problem/723/D
      2 
      3 D. Lakes in Berland
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The map of Berland is a rectangle of the size *n* × *m*, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean.
     22 
     23 Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.
     24 
     25 You task is to fill up with the earth the minimum number of water cells so that there will be exactly *k* lakes in Berland. Note that the initial number of lakes on the map is not less than *k*.
     26 
     27 Input
     28 
     29 The first line of the input contains three integers *n*, *m* and *k* (1 ≤ *n*, *m* ≤ 50, 0 ≤ *k* ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.
     30 
     31 The next *n* lines contain *m* characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land).
     32 
     33 It is guaranteed that the map contain at least *k* lakes.
     34 
     35 Output
     36 
     37 In the first line print the minimum number of cells which should be transformed from water to land.
     38 
     39 In the next *n* lines print *m* symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.
     40 
     41 It is guaranteed that the answer exists on the given data.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 5 4 1  
     49 ****  
     50 *..*  
     51 ****  
     52 **.*  
     53 ..**
     54 ```
     55 
     56 Output
     57 
     58 ```
     59 1  
     60 ****  
     61 *..*  
     62 ****  
     63 ****  
     64 ..**
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 3 3 0  
     71 ***  
     72 *.*  
     73 ***
     74 ```
     75 
     76 Output
     77 
     78 ```
     79 1  
     80 ***  
     81 ***  
     82 ***
     83 ```
     84 
     85 Note
     86 
     87 In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.