列表

详情


NC51276. Muddy Fields

描述

Rain has pummeled the cows' field, a rectangular grid of R rows and C columns . While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat.
To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field.
Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other.
Compute the minimum number of boards FJ requires to cover all the mud in the field.

输入描述

* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

输出描述

* Line 1: A single integer representing the number of boards FJ needs.

示例1

输入:

4 4
*.*.
.***
***.
..*.

输出:

4

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 2ms, 内存消耗: 508K, 提交时间: 2020-08-14 15:15:00

#include<bits/stdc++.h>
using namespace std;
const int N = 56;
int n, m, tot = 1, a[N][N][2], f[N * N], ans;
char s[N][N];
bool v[N * N];
vector<int> e[N * N];

bool dfs(int x) {
	for (unsigned int i = 0; i < e[x].size(); i++) {
		int y = e[x][i];
		if (v[y]) continue;
		v[y] = 1;
		if (!f[y] || dfs(f[y])) {
			f[y] = x;
			return 1;
		}
	}
	return 0;
}

int main() {
	//freopen("in.txt", "r", stdin);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m + 1; j++)//m + 1
			if (s[i][j] == '*') a[i][j][0] = tot;
			else ++tot;
	int t = tot;
	for (int j = 1; j <= m; j++)
		for (int i = 1; i <= n + 1; i++)
			if (s[i][j] == '*') a[i][j][1] = tot;
			else ++tot;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (s[i][j] == '*') {
				e[a[i][j][0]].push_back(a[i][j][1]);
				e[a[i][j][1]].push_back(a[i][j][0]);
			}

	for (int i = 1; i < t; i++) {
		memset(v, 0, sizeof(v));
		ans += dfs(i);
	}
	cout << ans << endl;
}

C++(clang++11) 解法, 执行用时: 5ms, 内存消耗: 476K, 提交时间: 2020-10-31 21:12:40

#include<bits/stdc++.h>
using namespace std;
const int N = 51;
int n, m, tot = 1, a[N][N][2], f[N * N], ans;
char s[N][N];
bool v[N * N];
vector<int> e[N * N];

bool dfs(int x) {
	for (unsigned int i = 0; i < e[x].size(); i++) {
		int y = e[x][i];
		if (v[y]) continue;
		v[y] = 1;
		if (!f[y] || dfs(f[y])) {
			f[y] = x;
			return 1;
		}
	}
	return 0;
}

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m + 1; j++)
			if (s[i][j] == '*') a[i][j][0] = tot;
			else ++tot;
	int t = tot;
	for (int j = 1; j <= m; j++)
		for (int i = 1; i <= n + 1; i++)
			if (s[i][j] == '*') a[i][j][1] = tot;
			else ++tot;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (s[i][j] == '*') {
				e[a[i][j][0]].push_back(a[i][j][1]);
				e[a[i][j][1]].push_back(a[i][j][0]);
			}

	for (int i = 1; i < t; i++) {
		memset(v, 0, sizeof(v));
		ans += dfs(i);
	}
	cout << ans << endl;
    return 0;
}

上一题