|洛谷|NOIP2010|搜索|P1378 油滴扩展

https://www.luogu.org/problem/show?pid=1378

比较简单,不过写错两个地方调了很久。。具体看代码

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
const double pi = 3.1415926535;
int n;  
int xa,xb,ya,yb;
int xi[10], yi[10];
double dis[10][10];
int visit[10];
double ans = 0.0;
double r[10];
double abss(double x) {return x>0?x:-x;} 
int dfs(int a, double s)
{
    if (a > n)
    {
        ans = max(ans, s);//改正,之前min是错误的
        return 0;
    }
    for (int i=1;i<=n;i++)
    {
        if (!visit[i])
        {
            visit[i] = true;
            r[i] = min(min(abss(xi[i]-xa), abss(xi[i]-xb)),min(abss(yi[i]-ya), abss(yi[i]-yb)));
            for (int j=1;j<=n;j++)
            {
                if (visit[j]&&j!=i)//改正,不是!visit[j]而是visit[j]
                {
                    r[i] = min(r[i], dis[i][j]-r[j]);
                }
            } 
            if (r[i]<=0) r[i] = 0; 
            dfs(a+1, s+pi*r[i]*r[i]);
            visit[i] = false;
        }
    }
}
int main()
{
    scanf("%d%d%d%d%d", &n ,&xa,&ya,&xb,&yb);
    for (int i=1;i<=n;i++)
    {
        scanf("%d%d", &xi[i], &yi[i]);
    }
    for (int i=1;i<=n;i++)
    for (int j=1;j<=n;j++)
    {
        dis[i][j] = sqrt((xi[i]-xi[j])*(xi[i]-xi[j])+(yi[i]-yi[j])*(yi[i]-yi[j]));
    }
    ms(visit,false);
    dfs(1, 0.0);
    printf("%d", (int)(abss(xa-xb)*abss(ya-yb)) - (int)(ans+0.5));
    return 0;
}


你可能感兴趣的:(搜索,洛谷)