codeforces723D Lakes in Berland 搜索

D. Lakes in Berland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 nm and k (1 ≤ n, m ≤ 500 ≤ 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.


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>

using namespace std;

struct node {
	int x, y;
	int area;
	node(int x, int y, int area) : x(x), y(y), area(area) {}
	node(int x, int y) : x(x), y(y) {}
	node() {}
	bool operator < (const node& on) const {
		return this->area < on.area;
	}
};

int n, m, k;
char mapp[55][55];
int vis[55][55];
int dir[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };

vector<node> v;

bool check(int x, int y) {
	if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y]) {
		return false;
	}
	return true;
}

bool checkf(int x, int y) {
	if (x < 0 || x >= n || y < 0 || y >= m || mapp[x][y] == '*' || vis[x][y] == 2) {
		return false;
	}
	return true;
}

//排除靠海水域
void bfso(int x, int y) {
	queue<node> Q;
	Q.push(node(x, y));
	vis[x][y] = true;
	while (!Q.empty()) {
		node tn = Q.front(); Q.pop();
		for (int i = 0; i < 4; i++) {
			int tx = tn.x + dir[i][0], ty = tn.y + dir[i][1];
			if (check(tx, ty)) {
				Q.push(node(tx, ty));
				vis[tx][ty] = true;
			}
		}
	}
}

//算面积,保存湖的位置
void bfs(int x, int y) {
	queue<node> Q;
	int maxa = 0;
	Q.push(node(x, y));
	vis[x][y] = true;
	while (!Q.empty()) {
		node tn = Q.front(); Q.pop();
		maxa++;
		for (int i = 0; i < 4; i++) {
			int tx = tn.x + dir[i][0], ty = tn.y + dir[i][1];
			if (check(tx, ty)) {
				Q.push(node(tx, ty));
				vis[tx][ty] = true;
			}
		}
	}
	//将搜素的湖的位置和面积保存下来,用于后续排序和填充
	v.push_back(node(x, y, maxa));
}

//用陆地填充湖
void Fill(int x, int y) {
	queue<node> Q;
	Q.push(node(x, y));
	mapp[x][y] = '*';
	vis[x][y] = 2;
	while (!Q.empty()) {
		node tn = Q.front(); Q.pop();
		for (int i = 0; i < 4; i++) {
			int tx = tn.x + dir[i][0], ty = tn.y + dir[i][1];
			if (checkf(tx, ty)) {
				Q.push(node(tx, ty));
				mapp[tx][ty] = '*';
				vis[tx][ty] = 2;
			}
		}
	}
}

int main()
{
	cin >> n >> m >> k;
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++) {
		scanf("%s", mapp[i]);
		for (int j = 0; j < m; j++) {
			if (mapp[i][j] == '*') {
				vis[i][j] = true;
			}
		}
	}
	//把边缘靠海的水域排除掉
	for (int i = 0; i < n; i++) {
		if (!vis[i][0]) {
			bfso(i, 0);
		}
		if (!vis[i][m - 1]) {
			bfso(i, m - 1);
		}
	}
	for (int i = 0; i < m; i++) {
		if (!vis[0][i]) {
			bfso(0, i);
		}
		if (!vis[n - 1][i]) {
			bfso(n - 1, i);
		}
	}

	v.clear();
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!vis[i][j]) {
				bfs(i, j);
			}
		}
	}
	//从小到大排序,尽可能先填充面积小的
	sort(v.begin(), v.end());

	int ans = 0, len = v.size();
	for (int i = 0; i < len - k; i++) {
		ans += v[i].area;
		Fill(v[i].x, v[i].y);
	}
	cout << ans << endl;
	for (int i = 0; i < n; i++) {
		puts(mapp[i]);
	}
	return 0;
}






### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值