日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【29.70%】【codeforces 723D】Lakes in Berland

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【29.70%】【codeforces 723D】Lakes in Berland 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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.

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.

Input
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.

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).

It is guaranteed that the map contain at least k lakes.

Output
In the first line print the minimum number of cells which should be transformed from water to land.

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.

It is guaranteed that the answer exists on the given data.

Examples
input
5 4 1


..


*.
..**
output
1


..



..**
input
3 3 0


.


output
1




Note
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.

【題解】

題意:和邊相連的連通塊不算lake,一開始有s個lake(s>=k),求最少要去掉多少個cell of lake才能讓總的lake個數變為k;
先預處理將那些邊上的連通塊給置為1表示置為平地。但是要記錄一下這些點的坐標。最后輸出的時候還是要變回來。
然后i=1->n;j=1->m;尋找連通塊。并記錄各個連通塊的起始坐標。獲取這個連通塊的cell個數設為size;
以size為關鍵字升序排序;
看需要減少多少個湖。就從小到大累加相應個數的size;
根據記錄的起始坐標再次進行bfs(需要在一個初始且去掉邊上的連通塊的a數組的copy數組cpa進行);
最后把一開始去掉的邊上的連通塊重新置為是cell of lake;
最后輸出整個矩陣;

#include <cstdio> #include <queue> #include <vector> #include <algorithm>using namespace std;const int MAXN = 55; const int dx[5] = { 0,1,-1,0,0 }; const int dy[5] = { 0,0,0,1,-1 };int n, m,k,total = 0; bool a[MAXN][MAXN] = { 0 }, cpa[MAXN][MAXN] = { 0 }; char s[MAXN]; struct data2 {int x,y;int size; };queue < data2 > dl; vector < pair<int, int> > rest; data2 qidian[MAXN*MAXN]; int cnt = 0;void bfs1(int a0, int b0) {rest.push_back(make_pair(a0, b0));a[a0][b0] = false;data2 temp;temp.x = a0, temp.y = b0;dl.push(temp);while (!dl.empty()){data2 temp1;temp1 = dl.front();int a1 = temp1.x, b1 = temp1.y;dl.pop();for (int i = 1; i <= 4; i++){int a2 = a1 + dx[i];int b2 = b1 + dy[i];if (a[a2][b2]){a[a2][b2] = false;rest.push_back(make_pair(a2, b2));data2 temp2;temp2.x = a2;temp2.y = b2;dl.push(temp2);}}} }int bfs2(int a0, int b0,bool a[MAXN][MAXN]) {int num = 1;a[a0][b0] = false;data2 temp;temp.x = a0, temp.y = b0;dl.push(temp);while (!dl.empty()){data2 temp1;temp1 = dl.front();int a1 = temp1.x, b1 = temp1.y;dl.pop();for (int i = 1; i <= 4; i++){int a2 = a1 + dx[i];int b2 = b1 + dy[i];if (a[a2][b2]){a[a2][b2] = false;num++;data2 temp2;temp2.x = a2;temp2.y = b2;dl.push(temp2);}}}return num; }bool cmp(data2 a, data2 b) {return a.size < b.size; }int main() {//freopen("F:\\rush.txt", "r", stdin);scanf("%d%d%d", &n, &m, &k);for (int i = 1; i <= n; i++){scanf("%s", s);for (int j = 1; j <= m; j++)if (s[j - 1] == '.')a[i][j] = 1;elsea[i][j] = 0;}for (int i = 1; i <= m; i++)//去掉邊上的連通塊。并記錄那些連通塊的每個cell的坐標之后方便回溯if (a[1][i])bfs1(1, i);for (int i = 1; i <= m; i++)if (a[n][i])bfs1(n, i);for (int i = 1; i <= n; i++)if (a[i][1])bfs1(i, 1);for (int i = 1; i <= n; i++)if (a[i][m])bfs1(i, m);for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)//copy一下a數組cpa[i][j] = a[i][j];//這個cpa數組用來最后輸出答案。刪除操作也在這上面進行for (int i = 1;i <= n;i++)for (int j = 1; j <= m; j++)if (a[i][j]){cnt++;qidian[cnt].x = i; qidian[cnt].y = j;//記錄這個湖的起點坐標qidian[cnt].size = bfs2(i, j,a);}total = cnt - k;sort(qidian + 1, qidian + 1 + cnt, cmp);//以湖的大小為關鍵字升序排int i = 0,zs = 0;while (total > 0)//看需要去掉幾個湖{i++;zs += qidian[i].size;bfs2(qidian[i].x, qidian[i].y, cpa);total--;}int len = rest.size();for (int i = 0; i <= len - 1; i++)//把之前置為平地的重新置為cell of lake{int x = rest[i].first, y = rest[i].second;cpa[x][y] = 1;}printf("%d\n", zs);for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++)if (cpa[i][j] == 1)putchar('.');elseputchar('*');printf("\n");}return 0; }

轉載于:https://www.cnblogs.com/AWCXV/p/7632186.html

總結

以上是生活随笔為你收集整理的【29.70%】【codeforces 723D】Lakes in Berland的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。