题目链接:P1378 油滴扩展
又是一道绿题,但是感觉难度和黄题差不多
我们直接看题:
数据非常小 直接考虑搜索
读懂题目:
题意大概图像是这样的:
给定一个区域 然后有很多个圆 求除了圆以外的面积
题目唯一难点:圆有可能在另外一个圆的里面,此时要排除这个情况;
直接上代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
//变量解释:n 点的个数,xx,yy,x2,y2 地图大小 ,pos 存点,r 存半径, st标记该点是否使用
int n;
int xx,yy ,x2, y2;
int ans = 0;
typedef pair<int, int> PII;
PII pos[N];
double PI = 3.1415926535;
double r[N];
bool st[N];
//计算两点距离
double jl(PII x, PII y) {
double a = pow(x.first - y.first,2);
double b = pow(x.second - y.second, 2);
return sqrt(a+b);
}
//计算总和
void js() {
double res = 0;
for (int i = 1; i <= n; i++) {
res += PI * r[i] * r[i];
}
//加0.5就能判断是否四舍五入
ans = max(int(res + 0.5),ans);
}
//求当前点半径
double bj(int x) {
double q = 1e9;
q = min(q, (double)abs(pos[x].first - xx));
q = min(q, (double)abs(pos[x].first - x2));
q = min(q, (double)abs(pos[x].second - yy));
q = min(q ,(double)abs(pos[x].second - y2));
for (int i = 1; i <= n; i++) {
if (i != x && r[i] != 0) {
q = min(q, max(jl(pos[x], pos[i]) - r[i],0.0));
}
}
return q;
}
void dfs(int x) {
if (x == n) {
js();
}
for (int i = 1; i <= n; i++) {
if (!st[i]) {
st[i] = 1;
r[i] = bj(i);
dfs(x + 1);
r[i] = 0;
st[i] = 0;
}
}
}
int main() {
cin >> n;
cin >> xx >> yy >> x2 >> y2;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
pos[i] = { x,y };
}
dfs(0);
int ss = abs(x2 - xx) * abs(y2 - yy);
cout <<ss- ans;
}
此题dfs难度比较小 思路很好想,就是细节比较多 ,数学方面也比较简单