跳到正文

题解

题解:P15836 [蓝桥杯第一届国际赛] 捕鱼达人

_initializer_list_约 8 分钟
原文
文章信息与版本

文章信息

收录
2026年7月30日
更新
2026年7月30日
篇幅
约 8 分钟
题目
P15836

版本

  1. v3当前
  2. v2
  3. v1

题目传送门

思路

首先,题目说:

两个点 (x1,y1)(x_1, y_1) 和 (x2,y2)(x_2, y_2) 的欧式距离定义为 (x1−x2)2+(y1−y2)2\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}。

那么我们就有了计算两点间距离的函数 calc:

C++
double calc(node u, node v) {
	double dx = u.x - v.x;
	double dy = u.y - v.y;
	return sqrt(dx * dx + dy * dy);
}

考虑爆搜 + 一点点剪枝和优化,代码如下:

爆搜代码
C++
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define rep(x, y, z) for (int x = (y); x <= (z); ++x)
#define per(x, y, z) for (int x = (y); x >= (z); --x)
#define endl '\n'
inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }

const int N = 55;
struct node {
	double x, y;
} a[N];
int n, r, ans, rm;
bool vis[N];
double calc(node u, node v) {
	double dx = u.x - v.x;
	double dy = u.y - v.y;
	return sqrt(dx * dx + dy * dy);
}
void dfs(int cur) {
	if (cur >= ans) return;
	if (!rm) { //如果剩下的鱼数量为0,那么更新答案
		ans = cur;
		return;
	}
	rep(i, 1, n) {
		if (vis[i]) continue;
		vector<int> v; //存储有效的渔网下标
		rep(j, 1, n) {
			if (!vis[j] && calc(a[i], a[j]) <= r) { //满足条件就标记为true并加入vector
				vis[j] = true;
				v.push_back(j);
			}
		}
		rm -= v.size(); //计算剩余鱼数
		dfs(cur + 1); //递归遍历
		rm += v.size(); //回溯
		for (int j : v) vis[j] = false;
	}
}
int main() {
    fast_io();
	cin >> n >> r;
	rep(i, 1, n) {
		cin >> a[i].x >> a[i].y;
		vis[i] = false;
	}
	ans = rm = n; //初始答案和剩余数均为n
	dfs(0);
	cout << ans;
    return 0;
}

那么这个能否 AC 呢? 显然是不能的。 因为 dfs 会有很多重复搜索,导致复杂度飙升到 O(2n)\mathcal{O(2^n)},而题目 nn 最大 5050,显然 TLE。那么继续优化:

优化后的代码
C++
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define rep(x, y, z) for (int x = (y); x <= (z); ++x)
#define per(x, y, z) for (int x = (y); x >= (z); --x)
#define endl '\n'
inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }

const int N = 55;
struct node {
	double x, y;
} a[N];
int n, r, ans, rm;
bool vis[N];
double calc(node u, node v) {
	double dx = u.x - v.x;
	double dy = u.y - v.y;
	return sqrt(dx * dx + dy * dy);
}
void dfs(int cur, int st) { //新增参数st,表示从哪里为起点进行dfs,不会搜到重复的
	if (cur >= ans) return;
	if (!rm) {
		ans = cur;
		return;
	}
	rep(i, st, n) {
		if (vis[i]) continue;
		vector<int> v;
		rep(j, 1, n) {
			if (!vis[j] && calc(a[i], a[j]) <= r) {
				vis[j] = true;
				v.push_back(j);
			}
		}
		rm -= v.size();
		dfs(cur + 1, i + 1);
		rm += v.size();
		for (int j : v) vis[j] = false;
	}
}
int main() {
    fast_io();
	cin >> n >> r;
	rep(i, 1, n) {
		cin >> a[i].x >> a[i].y;
		vis[i] = false;
	}
	ans = rm = n;
	dfs(0, 1);
	cout << ans;
    return 0;
}

可终究还是无法改变 TLE 的结局。

那么我们有什么办法能够 AC 呢?没错———状压 dfs。我们使用 prepre 数组存储状态,其中 preipre_i 的 第 jj 位若为 11 表示该鱼已被渔网覆盖,反之则没被覆盖。我们预先算出在每条鱼位置放网能够覆盖哪些鱼,这样能大大增加运行效率。随后 dfs 尝试选择渔网,不断扩大覆盖集合,强剪枝减少递归次数,便可 AC。

代码

AC Code
C++
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define rep(x, y, z) for (int x = (y); x <= (z); ++x)
#define per(x, y, z) for (int x = (y); x >= (z); --x)
#define endl '\n'
inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }

const int N = 55;
struct node { 
	double x, y;
} a[N];
ll n, ans;
double r;
ll pre[N];
double calc(node u, node v) {
	double dx = u.x - v.x;
	double dy = u.y - v.y;
	return dx * dx + dy * dy;
}
void dfs(ll cur, ll sta) { //原先的起点st参数改成状态传参
	if (cur >= ans) return; //剪枝,答案比目前最优解大或等于,没有必要继续搜索
	ll ful = (1LL << n) - 1; //计算全覆盖掩码(也就是把pre[i]所有位填1的结果)
	if (sta == ful) { //如果状态已经为全覆盖掩码(捕完了),就更新答案
		ans = cur;
		return;
	}
	ll t = __builtin_ctzll(~sta & ful); //GCC扩展,计算最低位1后面有多少个零,等价于最低位1的下标(本人已经试过暴力循环,成功TLE)
	rep(i, t, n - 1) { //从计算好的起点循环,减少大量无效递归
		if (!(pre[i] & (1LL << t))) continue;
		dfs(cur + 1, sta | pre[i]); //递归计算
	}
}
int main() {
    fast_io();
	cin >> n >> r;
	r *= r; //这里做平方照映前面calc函数,因为sqrt会带来一定的精度误差
	rep(i, 0, n - 1) { 
		cin >> a[i].x >> a[i].y;
	}
	ans = n; 
	rep(i, 0, n - 1) rep(j, 0, n - 1) if (calc(a[i], a[j]) <= r) pre[i] |= (1LL << j); //预处理
	dfs(0, 0);
	cout << ans; 
    return 0;
} //完结撒花
原文